When implementing HTTP/TCP/WebSocket/networking logic, treat protocol semantics as “correctness-critical”: don’t rely on accidental behavior, and don’t assume inputs are single-valued.

Apply these rules: 1) Be consistent about connection lifecycle

2) Defensively handle multi-value headers

3) Normalize outbound URLs

4) Avoid fragile query parsing/merging

5) Parse WebSocket subprotocol lists correctly

Example (multi-value header handling):

local function first_header_value(v)
  if type(v) == "table" then
    return v[1]
  end
  return v
end

local instana_t = first_header_value(headers["x-instana-t"])
local instana_s = first_header_value(headers["x-instana-s"])