code: public synchronized int getFreePort(int lastPortNumber)
          throws IOException {
          int id = 0;
          if (lastPortNumber != 0) {
              id = lastPortNumber - minPort;
              if (id < 0) {
                  throw new IOException("Port number out of range.");
              }
          }
          for(int i=id;i<ports.length;i++) {
              if (ports[i] == USED) continue;
              return minPort+i;
          }
          throw new IOException("No free ports available.");
      }
docs: Returns first available port.
  
  @param lastPortNumber port number to start finding the next
  available port from. Set it to 0 if
  called initialy.
  @return the next available port number from the lastPortNumber.
  @exception IOException if there is no more free ports available or
  if the lastPortNumber is incorrect.