The Handshake
The Story of How Two Strangers Agree to Talk
The Foundation of Everything
Every email you send. Every webpage you load. Every video you stream. Every message, every file, every transaction.
Before any of it can happen, two computers must agree to talk to each other.
This is the story of how that agreement happens — in three messages, taking less than 50 milliseconds, billions of times per second, across the entire internet.
It's called the TCP three-way handshake. And it's happening right now, as you read these words.
The Problem of Strangers
It's 2:34 PM. Priya types www.github.com into her browser and presses Enter.
Simple, right? She wants a webpage. GitHub has the webpage. What's the problem?
Here's the problem: Priya's laptop and GitHub's server have never met.
They are strangers. They exist on opposite sides of the planet — Priya in Mumbai, GitHub's servers in Virginia, USA. Between them lies an ocean, a dozen countries, and about thirty different network devices: routers, switches, firewalls.
And here's what makes it harder: the internet is unreliable.
Messages get lost. They arrive out of order. They get duplicated. They get corrupted. The internet is a chaotic place where anything can go wrong.
So before Priya's laptop can send any real data — before it can request the webpage — it needs to know:
Is the server even there?
Is it willing to talk to me?
Can we agree on how to communicate?
This is the problem the TCP handshake solves. It's a way for two strangers to agree to have a conversation — before the conversation begins.
The First Message is Born
Priya pressed Enter. Now, deep inside her laptop's operating system, the networking stack wakes up.
The browser said: "I need to connect to 140.82.121.3, port 443." (That's GitHub's IP address, and port 443 is for HTTPS.)
The operating system knows what to do. It builds a special packet — the opening message of the handshake.
Meet SYN-4847. Our protagonist.
Let's understand what's inside this packet.
Source Port (52431) — A random number Priya's laptop chose. It's like a return address, so the server knows where to send its reply.
Destination Port (443) — Where on the server Priya wants to connect. Port 443 is the standard door for secure web traffic.
Sequence Number (2847391056) — This is crucial. It's a random starting number that Priya's laptop chose. Every byte of data she sends later will be numbered starting from here. This prevents old, delayed packets from being confused with new ones.
SYN Flag = 1 — This is the magic bit. SYN stands for "synchronize." Setting this flag to 1 means: "I want to start a new connection. I want to synchronize our sequence numbers."
Window Size (65535) — How much data Priya's laptop can receive at once. It's like saying "I can handle up to 64KB before you need to wait for my acknowledgment."
SYN-4847 is small — about 60 bytes. But it carries a big message:
Priya's Laptop
"Hello. I exist. I want to talk. My sequence number starts at 2847391056. Will you talk to me?"
The packet is complete. Now it must travel.
Across the World in Milliseconds
SYN-4847 leaves Priya's laptop. It's wrapped in an IP packet (with source and destination addresses) and sent down the ethernet cable to her home router.
The router looks at the destination: 140.82.121.3. That's not on the local network. The router forwards the packet to Priya's ISP.
The ISP's router looks at the destination. Not in India. It forwards the packet toward an international gateway.
The packet crosses undersea cables. Fiber optic light pulses race beneath the Indian Ocean, around the Arabian Peninsula, through the Mediterranean, across the Atlantic.
Each router along the way makes the same decision: "Where does 140.82.121.3 live? Which direction should I send this?"
Hop 2: ISP Gateway Mumbai — 5ms
Hop 3: TATA International Gateway — 12ms
Hop 7: London IX — 89ms
Hop 11: New York IX — 142ms
Hop 14: GitHub Edge Network Virginia — 168ms
Fourteen hops. Thousands of miles. SYN-4847 has crossed an ocean.
The packet arrives at a massive data center in Ashburn, Virginia. It passes through load balancers, through firewalls, and finally reaches a server.
The server's network card receives the packet. It sees the SYN flag. It knows what this means.
Someone wants to connect.
The Server's Decision
The server examines SYN-4847. It sees:
Someone at 103.21.45.78 (Priya's public IP) wants to connect to port 443.
They want to start their sequence numbering at 2847391056.
They can receive up to 65535 bytes at a time.
The server has a choice. It can accept this connection request, or ignore it.
Is port 443 open? Yes — the web server is listening. Is there room for another connection? Yes — plenty of capacity. Is this IP address blocked? No — it's allowed.
The server decides: I will accept.
But it's not enough to just say "yes." The server needs to prove it received the SYN, and it needs to send its own sequence number. So it builds a special response — a packet that says both "I acknowledge your request" AND "here's my own request."
This is the famous SYN-ACK.
The Reply
Meet SYN-ACK-7712. The server's response.
This packet has two flags set: SYN and ACK. It's doing double duty.
The ACK part says: "I received your SYN. Your sequence number was 2847391056. I'm expecting your next byte to be 2847391057." (That's the original sequence number + 1.)
The SYN part says: "And here's MY sequence number: 1093827465. I want to synchronize too."
The server is saying:
GitHub's Server
"Yes, I heard you. I acknowledge your request. And here's my own starting number. Now we both know where each other begins."
SYN-ACK-7712 begins its journey back. Across the Atlantic, through the undersea cables, through the routers and gateways, back to Mumbai, back to Priya's laptop.
SYN-ACK-7712 arrives. Priya's laptop examines it.
The ACK flag is set. The acknowledgment number is 2847391057 — exactly what the laptop expected (its sequence number + 1). This proves the server received the original SYN.
The SYN flag is also set. The server wants to synchronize its own sequence number: 1093827465.
But the handshake isn't complete yet. The server sent its SYN, but doesn't know if Priya's laptop received it. The server is waiting, hoping, trusting that a response will come.
One more message is needed.
The Final Acknowledgment
Priya's laptop builds the final message of the handshake. This one is simple — just an acknowledgment.
Meet ACK-4848. The closer.
No SYN this time. Just ACK. Priya's laptop is saying:
Priya's Laptop
"I received your SYN-ACK. I acknowledge your sequence number. I'm expecting your first byte to be 1093827466."
ACK-4848 races across the world one more time.
The server receives it. It sees the ACK. It sees the acknowledgment number is correct.
Both sides have now confirmed that both sides are listening.
Connection Established
The connection is ESTABLISHED.
Both computers have agreed:
"Yes, you exist. Yes, I exist. We can hear each other. We know each other's sequence numbers. Let's talk."
Three messages. SYN, SYN-ACK, ACK. About 500 milliseconds total (thanks to the Mumbai-to-Virginia distance). A conversation between strangers, establishing trust, agreeing on the rules.
Now — and only now — can Priya's browser send its HTTP request: "GET / HTTP/1.1" to fetch the GitHub homepage.
The data can flow. Reliable, ordered, verified. Because both sides shook hands first.
What SYN-4847 Taught Us
A TCP connection requires both sides to agree. Neither can assume the other is listening. Neither can assume messages arrive. The three-way handshake proves — with mathematical certainty — that both sides can send AND receive.
One message proves the client can send.
Two messages proves the server can send and receive.
Three messages proves the client can receive.
With three messages, both directions of communication are verified. Less than three, and you can't be sure the full path works.
The internet can deliver packets out of order. Packet 5 might arrive before Packet 3. Old packets from a previous connection might show up late and cause confusion.
By choosing random starting sequence numbers, and incrementing them with each byte sent, TCP can:
- Put packets back in the correct order
- Detect missing packets
- Reject old, delayed packets from dead connections
Defined in: RFC 793 (1981)
Handshake: SYN → SYN-ACK → ACK
States: CLOSED → SYN_SENT → ESTABLISHED (client)
States: LISTEN → SYN_RECEIVED → ESTABLISHED (server)
Typical time: 1 RTT (Round Trip Time) to complete
Happens: Billions of times per second globally