code: public synchronized void messageReceived(final NextFilter nextFilter,
              final IoBuffer buf) {
          try {
              int step = (Integer) getSession().getAttribute(HANDSHAKE_STEP);
  
              if (step == SocksProxyConstants.SOCKS5_GREETING_STEP
                      && buf.get(0) != SocksProxyConstants.SOCKS_VERSION_5) {
                  throw new IllegalStateException(
                          "Wrong socks version running on server");
              }
  
              if ((step == SocksProxyConstants.SOCKS5_GREETING_STEP || 
                      step == SocksProxyConstants.SOCKS5_AUTH_STEP)
                      && buf.remaining() >= 2) {
                  handleResponse(nextFilter, buf, step);
              } else if (step == SocksProxyConstants.SOCKS5_REQUEST_STEP
                      && buf.remaining() >= 5) {
                  handleResponse(nextFilter, buf, step);
              }
          } catch (Exception ex) {
              closeSession("Proxy handshake failed: ", ex);
          }
      }
docs: Handles incoming data during the handshake process. Should consume only the
  handshake data from the buffer, leaving any extra data in place.
  
  @param nextFilter the next filter
  @param buf the buffered data received