The connection is established between two hosts by a method known as the 3-Way
Handshake. Three segments are transmitted before the two hosts are fully synchronised
and ready to transmit the data.
In reference to figure 4.1 and figure 4.2, a host who wishes to make a connection sends out a TCP segment with the SEQ flag set and the proposed initial sequence number in the sequence field, say sequence=X. The source port field will be filled with the host's IP address and port to respond to. The destination port field will be filled with the destination host's IP address and port number to access.
The destination machine recognises that its own IP address is in the destination field. It then checks the port to see if there is a process listening to that port, if not then a ``Connection Refused'' message is sent back. If there is a process, the TCP on the destination machine notes the sequence number X, and returns a segment with the SEQ and the ACK flags set. It also populates the sequence number field with its own value, say Y, and the acknowledgement field with the value X+1.
The source machine receives the segment, notes the value Y, and returns a segment
with the ACK flag set and the acknowledgement field set to Y+1.
The two hosts have now established connection, and the transfer of data may be started.