Opening and Closing TCP Connections: The Three-Way Handshake, FIN and Half-Close
How TCP opens a connection with SYN, SYN-ACK and ACK, closes it with FIN in each direction, keeps one direction open with a half-close, handles simultaneous open, chooses random initial sequence numbers, and retries a SYN that gets no answer, all measured in real captures.
kvi5hnuCode Sprout team member17 min readOn this page
A TCP connection has three phases: setup, data transfer and teardown. Setup and teardown are short, but they decide a lot: whether the connection exists at all, what sequence numbers each side starts from, which options are in force, and when each side may forget the connection. This article watches both ends of real connections in the lab and explains every segment.
The lab topology is the usual one: client (192.0.2.10) reaches server (198.51.100.10) through the router r1, which adds 20 ms of delay in each direction.
The three-way handshake
Opening a connection takes three segments:
- The client sends a SYN with its initial sequence number (ISN), and its options: MSS, window scale, SACK permitted, timestamps (article 60 explains each). This is the active open.
- The server, which was listening (the passive open), answers with a SYN-ACK: its own ISN, plus an acknowledgment of the client's SYN, which is the client's ISN plus 1.
- The client answers with an ACK of the server's SYN: the server's ISN plus 1.
Each side has now proved two things: that it can reach the other, and that it saw the other's ISN. Three segments are the minimum for both directions to be confirmed, since the SYN-ACK does double duty as the server's SYN and its ACK.
Here's a real connection in which the client sends 12 bytes, then closes its sending direction, and the server replies and closes. It was captured at both ends at the same time, so the arrows below are drawn between real capture timestamps:

And the same connection as tshark printed it at the client:
1 0.000000 192.0.2.10 → 198.51.100.10 TCP 74 56836 → 8080 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM WS=128
2 0.049107 198.51.100.10 → 192.0.2.10 TCP 74 8080 → 56836 [SYN, ACK] Seq=0 Ack=1 Win=65160 Len=0 MSS=1460 SACK_PERM WS=128
3 0.049153 192.0.2.10 → 198.51.100.10 TCP 66 56836 → 8080 [ACK] Seq=1 Ack=1 Win=64256 Len=0
4 0.049293 192.0.2.10 → 198.51.100.10 TCP 78 56836 → 8080 [PSH, ACK] Seq=1 Ack=1 Win=64256 Len=12
5 0.049329 192.0.2.10 → 198.51.100.10 TCP 66 56836 → 8080 [FIN, ACK] Seq=13 Ack=1 Win=64256 Len=0
6 0.092430 198.51.100.10 → 192.0.2.10 TCP 66 8080 → 56836 [ACK] Seq=1 Ack=13 Win=65152 Len=0
7 0.141342 198.51.100.10 → 192.0.2.10 TCP 66 8080 → 56836 [ACK] Seq=1 Ack=14 Win=65152 Len=0
8 0.381680 198.51.100.10 → 192.0.2.10 TCP 85 8080 → 56836 [PSH, ACK] Seq=1 Ack=14 Win=65152 Len=19
9 0.381772 192.0.2.10 → 198.51.100.10 TCP 66 56836 → 8080 [ACK] Seq=14 Ack=20 Win=64256 Len=0
10 0.382758 198.51.100.10 → 192.0.2.10 TCP 66 8080 → 56836 [FIN, ACK] Seq=20 Ack=14 Win=65152 Len=0
11 0.385758 192.0.2.10 → 198.51.100.10 TCP 66 56836 → 8080 [ACK] Seq=14 Ack=21 Win=64256 Len=0
Reading it from the top:
- Segments 1 to 3 are the handshake. The SYN-ACK arrived 49 ms after the SYN, which is this path's round-trip time: 40 ms of added delay plus a few milliseconds of processing and timer slack in the lab.
- Segment 4 carries data,
Len=12, sent right after the handshake's ACK. A client may send data in its very first ACK. - Segment 5 is the client's FIN. Its sequence number is 13, right after the data (bytes 1 to 12), and like a SYN, the FIN consumes one sequence number.
- Segments 6 and 7 acknowledge the data (
Ack=13) and then the FIN (Ack=14), about 49 ms apart. The server held back the second ACK briefly, hoping to combine it with something to send (delayed ACKs, article 68). - Segment 8 is the server's 19-byte reply, sent after the client's FIN. That's a half-close at work.
- Segments 10 and 11 are the server's FIN and the client's final ACK.
Closing: one FIN per direction
TCP is full-duplex, so each direction is closed separately. Sending a FIN means "I won't send any more data", nothing more. The other direction stays open until the other side sends its own FIN. A normal close therefore takes four segments: FIN, ACK, FIN, ACK. The ACK and the second FIN are often combined, making three.
The side that sends the first FIN does an active close, and the other side does a passive close. The difference matters: only the active closer waits in the TIME_WAIT state afterwards (article 61).
When the application on the receiving side reads past the end of the data, it sees end of file: recv() returns 0 bytes. That's how a program learns the peer has closed its direction.
Half-close
In the lab run, the client called shutdown(SHUT_WR) right after sending its request. That sends a FIN but keeps the receiving direction open: a half-close. Both sides' programs reported what they saw.
On the client, 150 ms after the shutdown, ss showed its socket waiting for the server to finish:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
FIN-WAIT-2 0 0 192.0.2.10:56836 198.51.100.10:8080
client: b'reply: HELLO SERVER'
On the server, after reading to end of file:
server: got 12 bytes, then end of file; still allowed to send
State Recv-Q Send-Q Local Address:Port Peer Address:Port
CLOSE-WAIT 0 0 198.51.100.10:8080 192.0.2.10:56836
In the ss lines, Recv-Q is data received but not yet read by the application and Send-Q is data sent but not yet acknowledged; both are 0 here, so nothing is stuck. The State column is what matters. The client sat in FIN-WAIT-2 (its FIN was acknowledged, and it was waiting for the server's FIN), while the server sat in CLOSE-WAIT (it had received a FIN and the application hadn't closed yet). Then the server sent its reply, and the client received it, even though the client had "closed" long before.
Half-close is useful when the server can't respond until it has all of the input. The classic example is ssh host sort < file: the client sends the file, half-closes, and sort produces output only after it sees end of file. In the sockets API:
shutdown(fd, SHUT_WR)sends a FIN and keeps reading.close(fd)releases the socket entirely. If nothing else holds the socket, the kernel sends a FIN, and any data that arrives afterwards is answered with a reset.
Simultaneous open
Rarely, both sides send a SYN to each other at the same moment, each to the other's specific port. TCP allows this, and the result is one connection, not two. Each side receives a SYN while waiting for a SYN-ACK, answers it with a SYN-ACK, and the connection opens after four segments instead of three.
In the lab, two programs bound fixed ports (5000 on the client, 6000 on the server) and called connect() toward each other at the same instant, with 50 ms of delay each way, so the SYNs crossed in flight. Both programs connected and exchanged a greeting:
192.0.2.10 connected, received: hi from 198.51.100.10
198.51.100.10 connected, received: hi from 192.0.2.10
The client's capture shows the crossing SYNs:
1 0.000000 192.0.2.10 → 198.51.100.10 TCP 74 5000 → 6000 [SYN] Seq=0 Win=64240 Len=0
2 0.054570 198.51.100.10 → 192.0.2.10 TCP 74 6000 → 5000 [SYN] Seq=0 Win=64240 Len=0
3 0.054574 192.0.2.10 → 198.51.100.10 TCP 74 [TCP Retransmission] 5000 → 6000 [SYN, ACK] Seq=0 Ack=1 Win=64240 Len=0
4 0.112293 198.51.100.10 → 192.0.2.10 TCP 74 [TCP Retransmission] 6000 → 5000 [SYN, ACK] Seq=0 Ack=1 Win=64240 Len=0
5 0.112300 192.0.2.10 → 198.51.100.10 TCP 78 [TCP Dup ACK 3#1] 5000 → 6000 [ACK] Seq=1 Ack=1 Win=64256 Len=0 SLE=0 SRE=1
6 0.112338 192.0.2.10 → 198.51.100.10 X11 84 5000 → 6000 [PSH, ACK] Seq=1 Ack=1 Win=64256 Len=18
Each side sent a SYN (frames 1 and 2), then answered the other's SYN with a SYN-ACK (frames 3 and 4). tshark labels the SYN-ACKs "retransmission", since they repeat the sequence number of each side's SYN. That label is a tool's guess, not a TCP event. Frame 5 is Linux's final ACK, which carries a duplicate-SACK block (SLE=0 SRE=1, the left and right edges of a SACK block, article 66) reporting that the client received the server's SYN twice, once in the bare SYN and once in the SYN-ACK. tshark decoded frame 6 as X11 only because port 6000 is X11's registered port.
Simultaneous open almost never happens by accident, because clients use random ephemeral ports. It's used on purpose by some peer-to-peer NAT traversal techniques, which article 36 covers.
Simultaneous close is more common: both applications close at once, and FINs cross. Each side receives a FIN while waiting for an ACK of its own, sends an ACK, and both end up in TIME_WAIT. Article 61 shows the states involved.
Initial sequence numbers
Why doesn't every connection start at sequence number 0? Because of old segments. A connection is identified only by its 4-tuple. If a connection closes and a new one with the same 4-tuple opens soon after, a delayed segment from the old one could arrive with a sequence number that looks valid in the new one, and corrupt its data.
The original 1981 design made the ISN a 32-bit counter that ticked every 4 microseconds, so a new incarnation of a connection would start far from where the old one stopped. That counter was predictable, and in the 1990s attackers used it: an off-path attacker who can guess the ISN can forge segments, including a spoofed connection from a trusted address.
Today's rule, specified in RFC 9293 (originally RFC 6528), combines both goals:
where M is a fine-grained clock and F is a keyed cryptographic hash. For a given 4-tuple, ISNs still increase with time, so old incarnations don't overlap. For an attacker who doesn't know the secret key, they're unpredictable. Here are the SYNs of five back-to-back connections from the client to the same server port:
0.000 s 192.0.2.10 port 52696 ··········S· ISN 3875114209
0.004 s 198.51.100.10 port 8080 ·······A··S· ISN 46424807
0.143 s 192.0.2.10 port 52700 ··········S· ISN 2199059348
0.146 s 198.51.100.10 port 8080 ·······A··S· ISN 670052033
0.284 s 192.0.2.10 port 52708 ··········S· ISN 3629833761
0.289 s 198.51.100.10 port 8080 ·······A··S· ISN 1162157111
0.423 s 192.0.2.10 port 52720 ··········S· ISN 4099302506
0.427 s 198.51.100.10 port 8080 ·······A··S· ISN 1775543866
0.554 s 192.0.2.10 port 52736 ··········S· ISN 2201034378
0.557 s 198.51.100.10 port 8080 ·······A··S· ISN 939988874
The dotted column is tshark's flag summary: one position per flag, with a dot for "off", so S alone is a SYN and A plus S is a SYN-ACK. The client's ISNs (on SYNs) and the server's (on SYN-ACKs) jump around the 32-bit space with no visible pattern. Each connection used a new client port, so each hashed differently.
Random ISNs are one of several defenses against forged segments. Others are random ephemeral ports, strict sequence-number checks on resets (article 62), and, most effective of all, encrypting and authenticating the data with TLS.
When the SYN gets no answer
If the server is down, or a firewall silently drops SYNs, the client resends its SYN with growing gaps until it gives up. In the lab, a firewall rule on r1 discarded everything to port 9999 without any reply. The client's settings, two Linux kernel parameters read with sysctl, and the result:
net.ipv4.tcp_syn_retries = 6
net.ipv4.tcp_syn_linear_timeouts = 4
connect() failed after 135.6 s: [Errno 110] Connection timed out
The capture of every SYN the client sent:
SYN 1 at 0.000 s (gap 0.000 s) seq 1971230686
SYN 2 at 1.063 s (gap 1.063 s) seq 1971230686
SYN 3 at 2.086 s (gap 1.023 s) seq 1971230686
SYN 4 at 3.109 s (gap 1.022 s) seq 1971230686
SYN 5 at 4.135 s (gap 1.026 s) seq 1971230686
SYN 6 at 5.160 s (gap 1.025 s) seq 1971230686
SYN 7 at 7.205 s (gap 2.045 s) seq 1971230686
SYN 8 at 11.237 s (gap 4.032 s) seq 1971230686
SYN 9 at 19.365 s (gap 8.128 s) seq 1971230686
SYN 10 at 35.756 s (gap 16.390 s) seq 1971230686
SYN 11 at 68.007 s (gap 32.251 s) seq 1971230686

The client sent the original SYN and then 10 retransmissions, all with the same sequence number. The initial timeout is 1 second (RFC 6298). Classic exponential backoff would double every gap: 1, 2, 4, 8 seconds and so on. Linux 6.5 and later start with tcp_syn_linear_timeouts (default 4) extra retries at the initial timeout, which helps when the first SYN or two were lost on a busy network, and then double: 2, 4, 8, 16, 32 seconds. After the last SYN, the client waited one more doubled interval (about 64 seconds) and then failed with ETIMEDOUT.
Why 10 retransmissions when tcp_syn_retries is 6? Linux turns the retry count into a time budget: the time that 6 doubling retries from a 1-second start would take, 1 + 2 + 4 + 8 + 16 + 32 + 64 = 127 seconds. It gives up at the first timeout after that budget is spent. The short linear retries at the start fit more SYNs into the same budget, so the total time stays about the same as before Linux 6.5.
A few practical points:
- Two minutes is a long time. Applications that talk to remote services usually set their own connect timeout, such as a few seconds, instead of relying on the kernel's.
- A refusal is fast, a black hole is slow. If the server is up but nothing listens on the port, it replies with a reset, and
connect()fails at once with "connection refused" (article 62). Only silence produces the long retry sequence above. - The settings are per host and per socket.
net.ipv4.tcp_syn_retrieschanges the number of retries for the whole host, and the Linux socket optionTCP_SYNCNTchanges it for one socket. Servers have a matching setting,tcp_synack_retries(default 5), for resending SYN-ACKs. - Other systems differ. Windows by default makes two SYN retransmissions, with a 3-second initial timeout and doubling, so its connection attempts give up after about 21 seconds. macOS uses its own schedule as well. Measure if it matters.
Connections through NAT and translators
Devices in the middle often take part in connection setup. A NAT or an IPv6/IPv4 translator (NAT64) rewrites addresses and ports, so it must also fix the TCP checksum, because the checksum covers the IP addresses through the pseudo-header. To know when a mapping can be created and removed, such devices watch for SYN, SYN-ACK, ACK and FIN segments and follow a simplified copy of TCP's state machine. If the device forgets the connection, for example after an idle timeout, the next segment is dropped or answered with a reset, even though both end hosts still think the connection is fine. Article 35 shows NAT state in detail, and article 71 shows how keepalives prevent idle timeouts.
Common questions
Why three segments and not two?
Each direction needs its own SYN and its own ACK. The server combines its SYN and its ACK into one segment, but the client's ACK of the server's SYN must still be sent, so the server knows its ISN was received. With only two segments, the server couldn't tell a fresh SYN from an old duplicate one.
Can data be sent in the SYN?
The rules allow it, but a normal server keeps the data until the handshake completes, so it gains nothing. TCP Fast Open (article 60) uses a cookie from an earlier connection so the server can safely pass SYN data to the application immediately, saving one round trip.
What's the difference between close() and shutdown()?
shutdown(SHUT_WR) sends a FIN but leaves the socket usable for reading, which is a half-close. close() releases the descriptor. When the last descriptor is closed, the kernel sends a FIN (or a reset if unread data is waiting), and the application can no longer read.
Why does my server have connections stuck in CLOSE-WAIT?
The client closed its side, and the server application hasn't called close(). TCP won't time this out. Find where the code handles end of file or errors and make sure the socket is always closed.
Is the ISN really random?
It's the sum of a clock and a keyed hash of the 4-tuple, so it looks random to anyone without the host's secret key. For a single 4-tuple, it still increases over time, which keeps old segments from fitting into a new incarnation of the connection.
Why does connect() take more than two minutes to fail?
The server's SYNs are being dropped silently, and the client is retrying with backoff. With Linux defaults, that's 10 retransmissions over about 68 seconds, plus one last wait. Use an application-level connect timeout, or lower tcp_syn_retries or TCP_SYNCNT, if you need a faster failure.
What does a simultaneous open look like in ss?
Each side moves from SYN-SENT to SYN-RECEIVED (it got a SYN while waiting) and then to ESTABLISHED; article 61 maps all of TCP's states. The intermediate state is too brief to see in practice, but the capture shows the four-segment pattern.
What happens if the third segment of the handshake, the client's ACK, is lost?
The client already considers the connection ESTABLISHED and may start sending. The server is still in SYN-RECEIVED, so it retransmits its SYN-ACK (up to tcp_synack_retries times on Linux), and the client answers each copy with another ACK. Usually, though, the client's first data segment arrives first: it carries the ACK flag and the right acknowledgment number, so it completes the handshake on the server by itself. Only a client that sends nothing, waiting for the server to speak first, depends on the SYN-ACK retransmission.
Summary
- A connection opens with SYN, SYN-ACK, ACK. Each SYN carries an ISN and options, and each SYN consumes one sequence number.
- Each direction closes with its own FIN, so a normal close takes four segments, or three when an ACK and a FIN are combined. The active closer ends in TIME_WAIT.
- A half-close (
shutdown(SHUT_WR)) sends a FIN and keeps receiving. In the lab, the client sat in FIN-WAIT-2 and the server in CLOSE-WAIT while the server sent its reply. - In a simultaneous open, crossing SYNs produce one connection in four segments.
- ISNs are a clock plus a keyed hash of the 4-tuple (RFC 9293), which avoids overlap with old connections and blocks guessing.
- With Linux defaults, an unanswered SYN is retried 10 times: five 1-second gaps, then 2, 4, 8, 16 and 32 seconds, and
connect()fails after about 136 seconds. A reset from a closed port fails immediately.
Practice questions
1. A client's SYN has sequence number 1000. What Acknowledgment Number does the SYN-ACK carry, and what's the sequence number of the client's first data byte?
Answer
The SYN-ACK acknowledges 1001, because the SYN consumes one sequence number. The client's first data byte has sequence number 1001.
2. In the lab capture, the client's FIN had Seq=13, and the server acknowledged it with Ack=14. Why 14?
Answer
The client's data was bytes 1 to 12, so the FIN occupies sequence number 13. Acknowledging the FIN means expecting byte 14 next.
3. A server sends a large response and then calls close(), while the client is still sending a request body the server never reads. What will the client likely see?
Answer
A reset. When an application closes a socket that still has unread received data, or receives more data after closing, the kernel answers with an RST instead of a normal FIN exchange. The client may get "connection reset by peer" and could lose the end of the response if it arrives after the reset. Servers avoid this by reading or discarding the rest of the request, or by using a half-close first.
4. Why is a predictable ISN dangerous, even though TCP checks sequence numbers?
Answer
TCP accepts a segment if its addresses, ports and sequence number fit the connection. An off-path attacker who can predict the ISN and guess the ports can forge segments the receiver accepts, for example to inject data or to complete a spoofed connection from a trusted address.
5. A client tries to reach a server whose firewall silently drops SYNs. A second client tries a server that's up but has nothing listening on the port. Which connect() fails faster, and with what error?
Answer
The second one, almost immediately, with "connection refused", because the server's TCP replies with a reset. The first one retries its SYN with backoff and fails with "connection timed out", after about 136 seconds with Linux defaults.
6. With tcp_syn_linear_timeouts=0 and tcp_syn_retries=6, what would the gaps between SYNs be, assuming a 1-second initial timeout?
Answer
Pure exponential backoff: 1, 2, 4, 8, 16 and 32 seconds, so the last SYN goes out at 63 seconds, and connect() fails after one more doubled wait, at about 127 seconds. This was the Linux behavior before version 6.5.
Previous: TCP's Service Model and Header: Segments, Sequence Numbers and Flags
Next: TCP Options: MSS, Window Scale, SACK, Timestamps, User Timeout, TCP-AO and Fast Open

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.