Just try bindto port, and if it does not check for EADDRINUSEat errno. This is the only way, since correctly, any such verification should be atomic. If you did a separate check and then tried to bind to the port after you found that it was not in use, another process might bind to the port for a period of time, again causing it to fail. Similarly, if you did a separate check and found that the port was already in use, the process that used it might close the port, exit, or fail over time, again making the wrong result.
(, , ) , , " -?" . , (, , ) .