The three-way handshake
Every TCP connection — every HTTPS request, database query, kubectl call — starts with the three-way handshake and ends with a FIN or RST exchange. Understanding the handshake, the socket states it creates, and the kernel queues behind it lets you read tcpdump output, tell a firewall drop from a dead service in seconds, explain why TIME_WAIT and CLOSE_WAIT pile up, defend against SYN floods, and reason about latency. This topic pairs with TCP/UDP (transport fundamentals) and TLS (what happens right after the handshake).
The exchange
- SYN — the client picks a random initial sequence number (ISN) and asks to open a connection, advertising its options.
- SYN-ACK — the server acknowledges the client's ISN (
ack = ISN_c + 1, since SYN consumes one sequence number) and sends its own ISN and options. - ACK — the client acknowledges the server's ISN (
ack = ISN_s + 1). Both sides are ESTABLISHED; data can flow. (The client may even include data with this ACK.)
The handshake lives at the transport layer (layer 4). UDP has no handshake at all.
Why sequence numbers are exchanged
TCP numbers every byte. Exchanging ISNs lets each side:
- order incoming bytes and detect gaps (loss) and duplicates;
- tell segments from this connection apart from delayed segments of an older connection with the same 5-tuple.
ISNs are randomised (RFC 6528) so an off-path attacker can't guess them and inject spoofed segments or hijack connections.
Why three steps, not two
Each side must send its ISN and receive confirmation that the other side got it. With two messages, the server would never learn whether the client received the server's ISN — and a delayed duplicate SYN from an old connection could make the server open a bogus connection. The third message (the client's ACK of the server's ISN) completes both confirmations. It's really four logical messages (SYN, ACK, SYN, ACK) with the middle two combined into the SYN-ACK.
Options negotiated in the handshake
| Option | Purpose |
|---|---|
| MSS (maximum segment size) | Largest TCP payload each side wants to receive — typically MTU − 40 (1460 for 1500 MTU; 8961 for 9001 jumbo frames in a VPC) |
| Window scale | Multiplier for the 16-bit window field so windows can exceed 64 KB — vital for high-bandwidth or high-latency links |
| SACK permitted | Allows selective acknowledgements so only missing segments are retransmitted |
| Timestamps | Better RTT measurement and protection against wrapped sequence numbers |
| TCP Fast Open cookie | Allows data in the SYN on repeat connections |
Options only appear in SYN/SYN-ACK, so middleboxes that strip them (some old firewalls) silently degrade performance for the whole connection.
What the ACK number means
The acknowledgement number is the next sequence number the receiver expects — cumulative: "I have every byte before this". ack=5001 means "I've received your SYN (5000); send byte 5001 next".