TCP Connection Close (4-Way)

How TCP tears down a connection in FIN → ACK → FIN → ACK, and why the closer waits in TIME_WAIT for 2MSL — animated sequence diagram.

Scenario

Speed
TCP TEARDOWNTIME_WAIT WAITClientServerClosing an already-ESTABLISHED connection. Either side may send the first FIN; usually the client does.2FIN seq=8003ACK ack=8014FIN seq=5005ACK ack=501The client waits in TIME_WAIT for 2×MSL (~30–120s) — a safety margin in case the last ACK was lost and the server retransmits FIN.
ClientTIME_WAIT
ServerCLOSED

Current step 6/6

The client waits in TIME_WAIT for 2×MSL (~30–120s) — a safety margin in case the last ACK was lost and the server retransmits FIN.

Round trips to first byte

1 RTT

FIN/ACK overlap in both directions — about one round trip

TCP segmentTLS handshakeApplication data

Numbers above arrows are the step order. In TLS 1.3, parts in {} are already encrypted — the certificate that was plaintext in 1.2 is hidden. Brackets [] mark conditional messages sent only with those cipher suites.

TCP teardown takes one more message than setup because 'I'm done sending' (FIN) and the peer's own FIN are separate events. The server ACKs the client's FIN immediately but may still have data to send — the CLOSE_WAIT half-close gap.

TIME_WAIT is the subtle part: after the final ACK the client holds the socket for 2×MSL. If that ACK is lost the server retransmits FIN, and a socket that vanished couldn't answer — leaving the server's close hanging.

On servers, a pile of CLOSE_WAIT sockets usually means the application received FIN but never called close() — a classic connection-leak symptom. TCP/TLS Handshake Visualizer

Frequently asked questions

Why does teardown need four messages, not three?
Setup can merge SYN+ACK into one message, but on close the peer can't FIN immediately upon ACKing — it may have data left to send, so its FIN goes out separately.
Are thousands of TIME_WAIT sockets a problem?
Yes — each holds a (local IP, port, remote IP, port) tuple, so services making many short connections can exhaust ports. Connection keep-alive/reuse is the real fix.
Can I skip teardown with RST?
It works but isn't recommended — the peer may lose buffered data and all normal close confirmation is bypassed.

Other scenarios