public void process(SelectionKey key) {
      try {
        ByteBuffer buf = ByteBuffer.allocate(1024);
        int result;
        while (0 < (result = sessionChannel.read(buf))) {
          buf.flip();
          messageReceived(new ByteArrayBuffer(buf.array(), buf.position(), buf.remaining()));
          if (result == 1024) {
            buf.rewind();
          } else {
            return;
          }
        }

        if (result == -1) {
          // EOF => remote closed the connection, cancel the selection key and close the channel.
          key.cancel();
          sessionChannel.close();
        }
      } catch (IOException e) {
        LOGGER.log(Level.INFO, "Could not write response to socket", e);
        key.cancel();
        safelyClose(sessionChannel);
      }
    }
 public final void process(SelectionKey key) throws IOException {
   try {
     UnixSocketChannel clientChannel = channel.accept();
     clientChannel.configureBlocking(false);
     clientChannel.register(
         selector, SelectionKey.OP_READ, new SshAgentSessionSocketHandler(clientChannel));
   } catch (IOException ex) {
     LOGGER.log(Level.WARNING, "failed to accept new connection", ex);
     safelyClose(channel);
     throw ex;
   }
 }
 @Override
 public String toString() {
   if (addr != null) {
     return ((UnixSocketAddress) addr).toString();
   }
   return inner.toString();
 }
 @Override
 public void connect(final SocketAddress endpoint, final int timeout) throws IOException {
   if (endpoint instanceof UnixSocketAddress) {
     addr = endpoint;
     inner.connect((UnixSocketAddress) endpoint);
     setAllSocketOptions();
   }
 }
 @Override
 protected void reply(Buffer buf) throws IOException {
   ByteBuffer b = ByteBuffer.wrap(buf.array(), buf.rpos(), buf.available());
   int result = sessionChannel.write(b);
   if (result < 0) {
     throw new IOException("Could not write response to socket");
   }
 }
 private void setSocketOption(final SocketOptionSetter s) throws SocketException {
   if (inner.isConnected()) {
     s.run();
   } else {
     if (!optionsToSet.offer(s)) {
       throw new SocketException("Failed to queue option");
     }
   }
 }
 @Override
 public InetAddress getInetAddress() {
   if (inner.isConnected()) {
     try {
       return InetAddress.getByName("localhost");
     } catch (UnknownHostException e) {
       return null;
     }
   }
   return null;
 }
 @Override
 public synchronized void close() throws IOException {
   if (lingerTime > 0) {
     boolean sleeping = true;
     while (sleeping) {
       try {
         wait(lingerTime * (long) 1000);
       } catch (InterruptedException e) {
       }
       sleeping = false;
     }
   }
   shutdownInput();
   shutdownOutput();
   inner.close();
 }
 public ApacheUnixSocket() throws IOException {
   this.inner = UnixSocketChannel.open();
   this.addr = null;
 }
 @Override
 public boolean isClosed() {
   return !inner.isOpen();
 }
 @Override
 public boolean isConnected() {
   return inner.isConnected();
 }
 @Override
 public void shutdownOutput() throws IOException {
   inner.shutdownOutput();
 }
 @Override
 public boolean getKeepAlive() throws SocketException {
   return inner.getKeepAlive();
 }
 @Override
 public synchronized int getSoTimeout() throws SocketException {
   return inner.getSoTimeout();
 }