The way TCP establishes a connection is through the use of different TCP control flags used in a very specific order.
Source:
TCP 3-Way Handshake Process - GeeksforGeeks
TCP control flags
- URG (short for urgent): A value of one here indicates that the segment is considered urgent and that the urgent pointer field has more data about this
- This feature of TCP has never really had widespread adoption and isn’t normally seen
- ACK (short for acknowledged)
- A value of one in this field means that the acknowledgment number should be examined
- PSH (short for push)
- The transmitting device wants the receiving device to push currently-buffered data to the applications on the receiving end as soon as possible
- It’s normally more meaningful to buffer data and send them in chunks as it’s reduces duplicate data / more reliant?
- but immediate responses are sometimes needed with small amount of data where the push flag comes in
- RST (short for reset)
- One of the sides in a TCP connection hasn’t been able to properly recover form a series of missing or malformed segments
- Can’t put together the segments, start sending from scratch again
- SYN (short for synchronize)
- It’s used when first establishing a TCP connection and makes sure the receiving end knows to examine the sequence number field
- FIN (short for finish)
- When this flag is set to one, it means the transmitting computer doesn’t have any more data to send and the connection can be closed


Handshake
A way for two devices to ensure that they’re speaking the same protocol and will be able to understand each other
After establishing a connection Computer A is free to send whatever data it wants to Computer B and vice versa.
Each segment sent in either direction should be responded to by a TCP segment with the ACK field sent. This way the other side always knows what has been received.
In order to close this connection we use the four-way handshake.