TCP is a connection-oriented, reliable delivery byte-stream transport layer
protocol. TCP connections contain three phases: connection establishment,
data transfer and connection termination.
Connection establishment (3-way handshake)
A 3-way handshake is used to establish a connection. During connection
establishment, parameters such as sequence numbers are initialized to help
ensure ordered delivery and robustness. Typically, a server opens a socket
and listens passively for a connection. The client-side of
a connection initiates an active open by sending an initial SYN segment
to the server as part of the 3-way handshake. The server-side should respond
to a valid SYN request with a SYN/ACK. Finally, the client-side should
respond to the server with an ACK,
completing the 3-way handshake.
Normally, the connection termination phase uses two 2-way handshakes, with
each side of the connection terminating independently. While one direction
is closed it is still allowed to transfer data in the other direction.
Termination for one direction is indicated by a FIN segment which must
be acknowledged with an ACK. Therefore, a typical teardown requires a pair
of FIN and ACK segments from each TCP endpoint.