code: public static int tryPort(int port) throws IOException {
          ServerSocket socket = new ServerSocket(port);
          int rtn = socket.getLocalPort();
          socket.close();
          return rtn;
      }
docs: Check whether the port is available to bind
  
  @param port port
  @return -1 means unavailable, otherwise available
  @throws IOException