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