code: private synchronized Optional<Integer> takePort(Optional<Integer> portStart, Optional<Integer> portEnd) {
      if (!portStart.isPresent() && !portEnd.isPresent()) {
        for (int i = 0; i < 65535; i++) {
          try {
            int port = this.portLocator.random();
            Boolean wasAssigned = assignedPorts.putIfAbsent(port, true);
            if (wasAssigned == null || !wasAssigned) {
              return Optional.of(port);
            }
          } catch (Exception ignored) {
          }
        }
      }
  
      for (int port = portStart.or(MINIMUM_PORT); port <= portEnd.or(MAXIMUM_PORT); port++) {
        try {
          this.portLocator.specific(port);
          Boolean wasAssigned = assignedPorts.putIfAbsent(port, true);
          if (wasAssigned == null || !wasAssigned) {
            return Optional.of(port);
          }
        } catch (Exception ignored) {
        }
      }
  
      throw new RuntimeException(String.format("No open port could be found for %s to %s", portStart, portEnd));
    }
docs: Finds an open port. {@param portStart} and {@param portEnd} can be absent
  
  ______________________________________________________
  | portStart | portEnd  | takenPort                   |
  |-----------|----------|-----------------------------|
  |  absent   | absent   | random                      |
  |  absent   | provided | 1024 < port <= portEnd      |
  |  provided | absent   | portStart <= port <= 65535  |
  |  provided | provided | portStart = port = portEnd  |
  ------------------------------------------------------
  
  @param portStart the inclusive starting port
  @param portEnd the inclusive ending port
  @return The selected open port.