A good analogy to a TCP connection is a telephone call.
When you wish to place a telephone call, you first look up the telephone number of the person you wish to call. If there are many people who can be reached at that telephone number, you also look up an extension number. You then lift up the receiver and dial the number. When the person you wish to reach has answered the phone, you talk to that person. That person talks to you, as well. When your conversation is complete, each person hangs up the telephone.
A TCP connection is very similar. First you look up the IP address (telephone number) of the computer you wish to contact. For a TCP connection, there are always (the potential for) many different services at that address. So you have to look up the port (extension number). You then create a socket (pick up the receiver) and dial the number (connect to the IP and socket). Then the program you wish to contact has accepted your connection (answered the phone) you can send and receive data from that computer (hold a conversation), and finally each side closes their socket (hangs up the phone).
When you make a telephone call, you don't have to worry about how your voice gets converted to an electrical signal, or how it gets switched to the telephone of the person that you're calling. The telephones and the telephone company takes care of all of these details for you.
Likewise, when you connect to another computer using TCP, you don't have to worry about the details of how your data gets split up into different datagrams, or how it ensures that the data gets put back together in the correct order, or even how it gets routed to the computer that you're connecting to. The sockets API takes care of all of these details for you.
The main procedures that you call from the sockets API are listed here with a brief explanation:
gethostbyname = Look up an IP address from a domain name (look up a person's telephone number)
getservbyname = Look up a port number from a service name (look up a person's extension number)
socket = create a socket (lift up the telephone handset)
connect = connect to a given IP address and port number (dial the telephone number)
bind = force the socket API to utilize a particular port or address. (doesn't really work with the telephone analogy, but this is used to tell the API which port number your socket will use on the local machine)
listen = Tell the socket API that you wish to receive connections (switching on the "ringer" on your telephone)
accept = Accept an incoming connection (answer a telephone call)
send = send data out over a socket (talk into your telephone handset)
recv = receive data from a socket (listen to your telephone handset)
close = close a socket (hang up the telephone)
Therefore, a typical TCP session looks like this:
|send() & recv()||send() & recv()|
The getservbyname() call asks the operating system which port number a server will accept connections on for a given service.
For the sake of an example, we'll use http, which is the protocol used to transport web pages across the internet.
The server will start by calling getservbyname to ask the operating system which port is used for the http requests over tcp. The getservbyname API will return port number 80, which happens to be the worldwide standard for the http service. The server will then call the socket() procedure, this socket will then be bound to port 80 by calling the bind() procedure. Now, when we call the listen() procedure, the socket API will cause port 80 to be "open" for receiving requests. The program then calls accept(), which waits for someone to connect to it. Once a client has connected, it can send and receive data. Finally, when it's done, it closes the connection.
The client first asks the user for the host to connect to. Once it has a response from the user, it checks to see if the user supplied an IP address or if the user actually supplied a host name. If the user supplied a host name, it calls gethostbyname to find the IP address of the host it needs to connect to. Now, it needs to know which port to connect to, so it calls getservbyname to ask the operating system which port is used for http. The operating system will, of course, return 80. Now, it creates a socket which it can use to talk to the server by calling the socket() procedure. It uses the connect() procedure to connect that socket to the server, who is hopefully waiting for it to connect. Now it can send & receive data from the server. And finally, it calls close() to disconnect.