SelectableChannel doConnect() throws IOException, InterruptedException {

      final ServerSocketChannel serverChannel = ServerSocketChannel.open();

      serverChannel.socket().setReceiveBufferSize(BUFFER_SIZE);
      serverChannel.configureBlocking(false);
      final ServerSocket serverSocket = serverChannel.socket();
      serverSocket.setReuseAddress(true);

      serverSocket.bind(details.address());

      // these can be run on this thread
      addPendingRegistration(
          new Runnable() {
            @Override
            public void run() {
              final Attached attached = new Attached();
              attached.connector = ServerConnector.this;
              try {
                serverChannel.register(TcpReplicator.this.selector, OP_ACCEPT, attached);
              } catch (ClosedChannelException e) {
                LOG.error("", e);
              }
            }
          });

      selector.wakeup();

      return serverChannel;
    }
    /** blocks until connected */
    @Override
    SelectableChannel doConnect() throws IOException, InterruptedException {
      boolean success = false;
      final SocketChannel socketChannel = SocketChannel.open();
      try {
        socketChannel.configureBlocking(false);
        socketChannel.socket().setReuseAddress(true);
        socketChannel.socket().setSoLinger(false, 0);
        socketChannel.socket().setSoTimeout(0);
        socketChannel.socket().setTcpNoDelay(true);

        try {
          socketChannel.connect(details.address());
        } catch (UnresolvedAddressException e) {
          this.connectLater();
        }

        // Under experiment, the concoction was found to be more successful if we
        // paused before registering the OP_CONNECT
        Thread.sleep(10);

        // the registration has be be run on the same thread as the selector
        addPendingRegistration(
            new Runnable() {
              @Override
              public void run() {

                final Attached attached = new Attached();
                attached.connector = ClientConnector.this;

                try {
                  socketChannel.register(selector, OP_CONNECT, attached);
                } catch (ClosedChannelException e) {
                  if (socketChannel.isOpen()) LOG.error("", e);
                }
              }
            });

        selector.wakeup();
        success = true;
        return socketChannel;

      } finally {
        if (!success) {
          try {
            try {
              socketChannel.socket().close();
            } catch (Exception e) {
              LOG.error("", e);
            }
            socketChannel.close();
          } catch (IOException e) {
            LOG.error("", e);
          }
        }
      }
    }