code: public static boolean isPortAvailable(int port) {
          try {
              ServerSocket socket = new ServerSocket(port);
              socket.close();
              return true;
          } catch (IOException e) {
              return false;
          }
      }
docs: See if a port is available for listening on by trying to listen
  on it and seeing if that works or fails.
  
  @param port port to listen to
  @return true if the port was available for listening on