Manfred provided an excellent answer, but there are a few more things to consider.
IPAddress ipAddress = Dns.GetHostEntry (Dns.GetHostName ()).AddressList;
May result in an ipv6 host address
will as Manfred points out give you a "dynamically" allocated port. You need to send this information to the other application in a manner simmilar to the FTP PORT
] command for an ipv4 end point and like the FTP EPRT
] command for ipv6. You are obviously free to implment your own command format; PORT and EPRT are just examples showing how it has been done before.
It goes like this:
1. Server listens on configured port
2. client connects
3. client allocates a port "dynamically" -
4. client listens on "dynamically" allocated port on a separate thread, or async
5. client sends endpoint info to server (like FTP PORT or FTP EPRT)
6. server connects back to client using the provided endpoint info