code: private DatagramSocket initRandomPortSocket() {
          int bindRetries = 5;
          int currentlyTriedPort = getRandomPortNumber(JainSipUtils.MIN_PORT_NUMBER, JainSipUtils.MAX_PORT_NUMBER);
  
          DatagramSocket resultSocket = null;
          // we'll first try to bind to a random port. if this fails we'll try
          // again (bindRetries times in all) until we find a free local port.
          for (int i = 0; i < bindRetries; i++) {
              try {
                  resultSocket = new DatagramSocket(currentlyTriedPort);
                  // we succeeded - break so that we don't try to bind again
                  break;
              } catch (SocketException exc) {
                  if (exc.getMessage().indexOf("Address already in use") == -1) {
                      logger.fatal("An exception occurred while trying to create" + "a local host discovery socket.", exc);
                      return null;
                  }
                  // port seems to be taken. try another one.
                  logger.debug("Port " + currentlyTriedPort + " seems in use.");
                  currentlyTriedPort = getRandomPortNumber(JainSipUtils.MIN_PORT_NUMBER, JainSipUtils.MAX_PORT_NUMBER);
                  logger.debug("Retrying bind on port " + currentlyTriedPort);
              }
          }
  
          return resultSocket;
      }
docs: Initializes and binds a socket that on a random port number. The method would try to bind on a random port and retry 5
  times until a free port is found.
  
  @return the socket that we have initialized on a randomport number.