How to check if libwebsockets server port is already taken?-Collection of common programming errors

I have an issue where two instances of libwebsockets server are trying to use the same port. The first instance is running OK but the second one is crashing when trying to execute libwebsocket_create_context(..):

  struct lws_context_creation_info info;
  memset(&info, 0, sizeof(info));
  info.port = 9000;
  info.protocols = protocols;
  info.extensions = libwebsocket_get_internal_extensions();
  info.gid = -1;
  info.uid = -1;
  struct libwebsocket_context *context;
  context = libwebsocket_create_context(&info);

Is there a way to check if the desired port is already taken?

try {} catch(..) {} are not able to catch the crash.