Certainly, let's dive into the details of how TCP connections are established and closed using TCP control flags:
TCP Control Flags and Their Purposes:

When the value of each Control flags is set to one (1) :
- URG (Urgent): Indicates that the segment contains urgent data. Not widely adopted in modern networking.
- ACK (Acknowledgment): Acknowledges receipt of data. The acknowledgement number field should be examined if set to 1.
- PSH (Push): Requests the receiving end to push buffered data to the application immediately.
- RST (Reset): Used to reset a connection when issues with data recovery occur.
- SYN (Synchronize): Initiates the establishment of a TCP connection. Used to synchronize sequence numbers, to make sure the receiving end knows to examine the sequence number field.
- FIN (Finish): Indicates the sender has no more data to send and wishes to close the connection.
Establishing a TCP Connection (Three-Way Handshake):

- Computer A (transmitter) sends a TCP segment to Computer B with the SYN flag set.
- Computer A initiates the connection and indicates its initial sequence number.
- Computer B (receiver) responds with a TCP segment with both the SYN and ACK flags set.
- Computer B acknowledges Computer A's SYN and sends its own SYN, indicating its initial sequence number.
- Computer A responds with a TCP segment with only the ACK flag set.
- Computer A acknowledges Computer B's SYN and completes the handshake.
- At this point, the TCP connection is established, and data transfer can begin.
Operating in Full Duplex:

- Because both sides have sent SYN/ACK pairs, the TCP connection operates in full duplex mode.
- Each side can send and receive data independently, and the sequence and acknowledgement numbers are used to ensure proper data flow.
Closing a TCP Connection (Four-Way Handshake):