private void bindNIOServerSocket(InetAddress inetAddr) throws IOException {
    while (true) {
      int port = HBaseTestingUtil.randomFreePort();
      InetSocketAddress addr = new InetSocketAddress(inetAddr, port);
      ServerSocketChannel channel = null;
      ServerSocket serverSocket = null;
      try {
        channel = ServerSocketChannel.open();
        serverSocket = channel.socket();
        serverSocket.bind(addr); // This does not work
        break;
      } catch (BindException ex) {
        // continue
      } finally {
        if (serverSocket != null) {
          serverSocket.close();
        }
        if (channel != null) {
          channel.close();
        }
      }
    }
  }
Creates a NIO ServerSocketChannel, and gets the ServerSocket from there. Then binds the
obtained socket. This fails on Windows with Oracle JDK1.6.0u33, if the passed InetAddress is a
IPv6 address. Works on Oracle JDK 1.7.
hbase-server/src/test/java/org/apache/hadoop/hbase/TestIPv6NIOServerSocketChannel.java