TCP uses a three-way handshake to establish and terminate connections. When a party wants to terminate a connection, it sends a FIN (finish) segment to the other party. The receiving party responds with an acknowledgment (ACK) segment indicating that it has received the FIN. The receiving party then closes its end of the connection and sends a FIN to the other party, which responds with an ACK indicating that it has received the FIN. Once both parties have sent and received FIN and ACK segments, the connection is terminated. This process ensures that all data has been delivered and acknowledged before the connection is closed. If either party fails to respond with an ACK or does not receive the final FIN and ACK segments, the connection may enter a “half-open” state where it is neither fully established nor fully terminated. To avoid this, TCP provides timeout mechanisms that ensure that any unacknowledged or missing segments are retransmitted until the connection is successfully terminated.