code: public void bind(DatagramChannel channel, int port) throws IOException {
          // select port if wildcarded
          if (port == PORT_ANY) {
              port = portManager.next();
          }
  
          // try bind
          IOException ex = null;
          for (int q = 0; q < 100; q++) {
              try {
                  channel.bind(new InetSocketAddress(bindAddress, port));
                  ex = null;
                  break;
              } catch (IOException e) {
                  ex = e;
                  logger.info("Failed trying to bind " + bindAddress + ":" + port);
                  port = portManager.next();
              }
          }
  
          if (ex != null) {
              throw ex;
          }
      }
docs: Binds socket to global bind address and specified port.
  
  @param channel the channel
  @param port the port to bind to
  @throws IOException