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);
      }
    }
 @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();
 }