Simon B's answer is a path you cannot verify, just try linking and handling the error case, if it is already in use.
Otherwise, you are in a race state when some other application may capture the port between your check so that it is free and your subsequent attempt to associate with it. This means that you still have to handle the possibility that your call cannot be connected, so checking in advance has not been achieved.
source share