code: public static boolean isTcpPortAvailable(int currentPort) {
          try {
              Socket socket = SocketFactory.getDefault().createSocket(InetAddress.getLocalHost(), currentPort);
              socket.close();
              return false;
          } catch (Throwable e) {
              return true;
          }
      }
docs: Check whether the given TCP port is available.
  
  @param currentPort The port
  @return True if it is