bindwill immediately and specifically inform you if the port is unavailable ( EADDRINUSE). Other options make no sense.
Since it seems that you really don't care about which port you are bound to, it seems to me that a better approach would be to binda port 0that binds to an available port. You can determine the port that your socket was connected to using sockaddr_in.
If, on the other hand, the desired port is within 1.1023, then you just need to reserve a port for your program. The system will not automatically assign a service to these ports. (I'm not sure if this is true on Windows.)
source
share