What a question! What normally happens is as follows.
- Connections are uni-directional (send on one, receive on the other).
- If you want to receive on port 1234, you create a listen on port 1234.
- If you want to send to another machine's port 1234 you create a send
socket and _often_ say "I don't care which of my ports I send from", so your
machine pick one at random (say 24536).
OK, now the more complicated bits.
- If you say "pick a send port at random", there are a set of about 1024
ports from 1 upwards which are never selected because these are user for
known purposes such as telnet, FTP, web (http) etc.
- Sometimes a connection is bi-directional so if someone connects to you on
port 1234 from their port 26475 (say), you send back along that same
connection (from your 1234 to their 26475).
- Lots of partners (potentially) can connect to your 1234 port (if you're
listening) and each connection is uniquely identified by 4 pieces of
information:
- your IP address
- your port
- their IP address
- their port.
Remember that if you have multiple cards, or are "multi-homed", you might
have more than one IP address!
Now there are advantages to wanting to not only listen on port 1234, but
also start connections from it. This is possible on many platforms BUT NOT
on Linux (or at least not the last time I looked!). There are various
"sockets options" which need to be set to do this and Linux is missing
support for one of them (SO_REUSEADDRPORT if I remember correctly).
So, the answer to your question is "depends on what you want to do and on
what platform and how you denote a port".
It might be simpler if you ask a specific question such as "I'm using
program XYZ and want to do THIS - can I?".
Finally, the web is awash with good web pages on sockets. All of the
information and terms above can be found by a quick Google.
Paul DS.