2026-06-09 / C# / .NET Core

TCP Connection Lifecycle: What Handshake and Teardown Solve

Understand TCP three-way handshake, four-way teardown, half-close, TIME_WAIT, and what to inspect when debugging connection issues.

.NETSystem Design

TCP Connection Lifecycle: What Handshake and Teardown Solve

TCP is not about sending as quickly as possible. It establishes a reliable bidirectional byte stream. The connection lifecycle answers three questions: whether both sides can send and receive, where sequence numbers start, and how both directions close cleanly.

What Must Be Confirmed Before Data Transfer

Before application data flows, both sides need to confirm that the client can reach the server, the server can reach the client, initial sequence numbers are exchanged, and both sides agree that the connection is usable.

sequenceDiagram
    participant C as Client
    participant S as Server
    C->>S: SYN, seq=x
    S-->>C: SYN + ACK, seq=y, ack=x+1
    C->>S: ACK, ack=y+1

Why Three Steps

The first SYN proves that the client can send to the server. The SYN+ACK proves that the server received the SYN and can send back. The final ACK proves that the client received the server side of the handshake.

Three steps are not arbitrary. They complete bidirectional reachability confirmation and initial sequence-number synchronization.

Why Not Two Steps

With only two steps, the server cannot know whether the client received its SYN+ACK. Delayed old SYN packets can also make stale connection attempts look valid. The final ACK tells the server that the client really saw the server response and is ready to enter the established state.

What ACK Means During Data Transfer

TCP is a byte-stream protocol. ACK confirms the next expected byte sequence number, not an application message. One application write is not necessarily one packet, and multiple packets may be read by the application at once.

That is why application protocols need their own message boundaries, such as length prefixes, delimiters, or fixed formats.

Why Teardown Often Takes Four Steps

TCP is full duplex. One side finishing its sends does not mean the other side is done. Each direction closes independently.

sequenceDiagram
    participant A as Active closer
    participant B as Passive closer
    A->>B: FIN
    B-->>A: ACK
    B-->>A: FIN
    A->>B: ACK

FIN means “I have no more data to send.” The ACK only confirms that one direction has closed. The passive side may still have data to send, so it sends its own FIN later.

Why TIME_WAIT Exists

After the active closer sends the final ACK, it usually enters TIME_WAIT. This state has two purposes: it allows the final ACK to be retransmitted if the peer resends FIN, and it lets delayed packets from the old connection disappear from the network.

TIME_WAIT is not automatically a bug. If there are many TIME_WAIT connections, first check who actively closes connections, whether short connections dominate, and whether Keep-Alive or connection pooling is configured correctly.

Debugging View

  • Slow connection setup: inspect DNS, latency, SYN retransmission, and server backlog.
  • Intermittent connection failures: inspect firewall rules, ephemeral port exhaustion, pool limits, and accept capacity.
  • Many TIME_WAIT sockets: inspect active closer behavior, short-connection ratio, and HTTP Keep-Alive.
  • Many CLOSE_WAIT sockets: the application likely received FIN but did not close its socket.
  • Read/write surprises: distinguish TCP connection behavior from application protocol framing.

Summary

The three-way handshake establishes bidirectional confirmation and sequence synchronization. The four-way teardown closes both directions of a full-duplex connection. TIME_WAIT protects the final ACK and prevents delayed old packets from polluting later connections.

TCP Connection Lifecycle: What Handshake and Teardown Solve | Remi Resume