code: private boolean portAvailable(final int port) {
      ServerSocket socket = null;
      try {
        socket = new ServerSocket(port);
        return true;
      } catch (IOException ignored) {
        return false;
      } finally {
        if (socket != null) {
          try {
            socket.close();
          } catch (IOException e) {
            log.error("Couldn't close socket on port {} when checking availability: {}", port, e);
          }
        }
      }
    }
docs: Check if the port is available on the host. This is racy but it's better than nothing.
  
  @param port Port number to check.
  
  @return True if port is available. False otherwise.