>> Oh, great. So there's more than one kind? <<
Each connected socket has a source ip address and a source port as well as a destination ip address and destination port. These four bits of information are used by the socket provider to associate a packet with a connection.
Typically, the socket provider automatically selects the source ip address (most computers only have one ip address) and source port when you call connect(). This means that the source port and ip address is usually totally transparent to your program. You can retrieve the source ip address and port after a socket is connected using getsockname().
You can also specify the source port and ip address of a socket before you call connect() with a call to bind(). However, due to the possibility of conflicts this is strongly discouraged:
Originally Posted by
MSDN
Binding to a specific port number other than port 0(ed's note: port 0 means use automatic selection) is discouraged for client applications, since there is a danger of conflicting with another socket already using that port number.
You can view source and destination information for current connections using the console command netstat -n. For example, this printout shows three connections from my computer to a website (actually a random ip address):
Code:
Proto Local Address Foreign Address State
TCP 192.168.0.55:2824 99.143.76.204:80 ESTABLISHED
TCP 192.168.0.55:2825 99.143.76.204:80 ESTABLISHED
TCP 192.168.0.55:2826 99.143.76.204:80 ESTABLISHED
Note the different source ports. All other things being equal (source address, destination address, destination port), the socket provider uses the source port to differentiate between these connections.
When one refers to a port, in relation to sockets, they are usually, but not always, referring to the destination port. Hence the confusion.