
In networking jargon, a "connection" (or "tcp connection") is
thought of like this

                           a tcp connection
        IP address O------------------------------O IP address
        Port number                                 Port number


A "connection" is a pair of "endpoints" and each endpoint is
an ip:port address pair. So a connection is a pair of pairs.


At each endpoint of a connection there is a socket.

        socket                                         socket
    +-------------+                                +-------------+
    |             |        a tcp connection        |             |
    | IP address  |O------------------------------O| IP address  |
    | Port number |                                | Port number |
    |             |                                |             |
    |             |                                |             |
    +-------------+                                +-------------+

 Each socket is "bound" to its end of the connection and
 is "connected" to the other end of the connection.


Know these terms:
   connection
   endpoint
   bind
   connect




Here are the steps used by a tcp server and client to establish
a connection communicate with each other over the connection.


Step 1) The tcp server process creates an unbound ServerSocket.
        This ServerSocket is not yet able to establish connections
        with clients.

                                                    ServerSocket
                                                   +-------------+
                                                   |             |
                                                  O| IP address  |
                                                   |             |
                                                   |             |
                                                   |             |
                                                   +-------------+


Step 2) The tcp server process binds its ServerSocket to a port number.
        This ServerSocket is not yet able to establish connections
        with clients.

                                                    ServerSocket
                                                   +-------------+
                                                   |             |
                                                  O| IP address  |
                                                   | Port number |
                                                   |             |
                                                   |             |
                                                   +-------------+


Step 3) The tcp server process calls the accept() method on
        its ServerSocket. The ServerSocket can now establish
        connections with clients.


Step 4) A tcp client process creates a (unbound) Socket.

        Socket                                      ServerSocket
    +-------------+                                +-------------+
    |             |                                |             |
    | IP address  |O                              O| IP address  |
    |             |                                | Port number |
    |             |                                |             |
    |             |                                |             |
    +-------------+                                +-------------+


Step 5) The client process asks its Socket to bind and connect
        to the server's ServerSocket. (This picture would be
        just before the server's accept() method returns.)

        Socket                                      ServerSocket
    +-------------+                                +-------------+
    |             |        tcp connection          |             |
    | IP address  |O------------------------------O| IP address  |
    | Port number |                                | Port number |
    |             |                                |             |
    |             |                                |             |
    +-------------+                                +-------------+


 Step 6) After the connection is established and accept() returns,
         the ServerSocket will have created a new communicating
         Socket and connected the client Socket to this new Socket.
         (This picture would be just after the server's accept()
         method returns.)

        Socket                                      ServerSocket
    +-------------+                                +-------------+
    |             |       tcp connection           |             |
    | IP address  |O------------------------+     O| IP address  |
    | Port number |                         |      | Port number |
    |             |                         |      |             |
    |             |                         |      |             |
    +-------------+                         |      +-------------+
                                            |
                                            |          Socket
                                            |      +-------------+
                                            |      |             |
                                            +-----O| IP address  |
                                                   | Port number |
                                                   |             |
                                                   |             |
                                                   +-------------+


Step 6a) If the server process is parallelized, then the server
         can once again call accept() on the ServerSocket and
         establish a simultaneous communicating connection with
         a second client. If the server process is not parallelized,
         then the ServerSocket cannot establish a communicating
         connection with another client while the server process
         is communicating with the current client. However, the
         ServerSocket can queue up (non-communicating) connections
         with new clients!


Step 7) The client and server processes get input and output
        streams from their communicating Socket objects.

        Socket                                      ServerSocket
    +-------------+                                +-------------+
    |             |       tcp connection           |             |
    | IP address  |O------------------------+     O| IP address  |
    | Port number |                         |      | Port number |
    |             |                         |      |             |
    |             |                         |      |             |
    +-------------+                         |      +-------------+
      /|\     \ /                           |
       |       |                            |          Socket
       |       |                            |      +-------------+
     out       in                           |      |             |
                                            +-----O| IP address  |
                                                   | Port number |
                                                   |             |
                                                   |             |
                                                   +-------------+
                                                     /|\     \ /
                                                      |       |
                                                      |       |
                                                    out       in


Step 7a) In the case where the server is not parallelized and
         can only handle one communicating connection at a time,
         if a second client establishes a connection with the
         server, then that second client can get input and
         output streams from its Socket object while that Socket
         is still connected to the server's ServerSocket. But the
         client Socket cannot communicate with the ServerSocket.
         If the client tries to read from its input stream or
         write to its output stream, then the client will block
         until the server returns from the accept() method and
         creates a communicating Socket. In other words, the
         client's Socket can move from Step 5 to Step 7 while
         the server's ServerSocket is stuck at Step 5.

        Socket                                      ServerSocket
    +-------------+                                +-------------+
    |             |        tcp connection          |             |
    | IP address  |O------------------------------O| IP address  |
    | Port number |                                | Port number |
    |             |                                |             |
    |             |                                |             |
    +-------------+                                +-------------+
      /|\     \ /
       |       |
       |       |
     out       in


Step 8) The client and server processes implement an application
        level protocol using the tcp connection and its associated
        streams. A Java client or server process will wrap the
        input and output byte stream with whatever higher level
        stream is appropriate for the agreed on protocol (a text
        protocol or a binary protocol, etc.).


Step 9) At some point, either the client or the server process closes
        its end of the tcp connection by calling close() on its Socket.
        When the other endpoint detects this, the other endpoint closes
        its end of the connection by calling close() on its Socket.
        (NOTE: Calling close() on a Socket is not the same as
               calling close() on the two socket streams.)

        Socket                                      ServerSocket
    +-------------+                                +-------------+
    |             |                                |             |
    | IP address  |O                              O| IP address  |
    | Port number |                                | Port number |
    |             |                                |             |
    |             |                                |             |
    +-------------+                                +-------------+
      /|\     \ /
       |       |                                       Socket
       |       |                                   +-------------+
     out       in                                  |             |
                                                  O| IP address  |
                                                   | Port number |
                                                   |             |
                                                   |             |
                                                   +-------------+
                                                     /|\     \ /
                                                      |       |
                                                      |       |
                                                    out       in


Step 10) The two unused Socket objects get garbage collected.


Step 11) The server process once again calls accept() on
         its ServerSocket and is ready to establish a new
         communicating connection with a client (that is,
         go to Step 3).

                                                    ServerSocket
                                                   +-------------+
                                                   |             |
                                                  O| IP address  |
                                                   | Port number |
                                                   |             |
                                                   |             |
                                                   +-------------+
