code: public static Set<Integer> getAvailablePorts(int fromPort, int toPort) {
          if (fromPort < MIN_PORT_NUMBER || toPort > MAX_PORT_NUMBER
                  || fromPort > toPort) {
              throw new IllegalArgumentException("Invalid port range: "
                      + fromPort + " ~ " + toPort);
          }
  
          Set<Integer> result = new TreeSet<>();
  
          for (int i = fromPort; i <= toPort; i++) {
              ServerSocket s = null;
  
              try {
                  s = new ServerSocket(i);
                  result.add(i);
              } catch (IOException e) {
                  // Do nothing
              } finally {
                  if (s != null) {
                      try {
                          s.close();
                      } catch (IOException e) {
                          /* should not be thrown */
                      }
                  }
              }
          }
  
          return result;
      }
docs: Returns the {@link Set} of currently avaliable port numbers ({@link Integer})
  between the specified port range.
  
  @throws IllegalArgumentException if port range is not between
  {@link #MIN_PORT_NUMBER} and {@link #MAX_PORT_NUMBER} or
  <code>fromPort</code> if greater than <code>toPort</code>.