code: static public boolean selectSocks5Authentication(InputStream in, 
                                                      OutputStream out,
                                                      int methodId)
                                                      throws IOException{
                                                      
        int num_methods = in.read();
        if (num_methods <= 0) return false;
        byte method_ids[] = new byte[num_methods];
        byte response[] = new byte[2];
        boolean found = false;
  
        response[0] = (byte) 5;    //SOCKS version
        response[1] = (byte) 0xFF; //Not found, we are pessimistic
  
        int bread = 0; //bytes read so far
        while(bread < num_methods)
           bread += in.read(method_ids,bread,num_methods-bread);
  
        for(int i=0;i<num_methods;++i)
           if(method_ids[i] == methodId){
             found = true;
             response[1] = (byte) methodId;
             break;
           }
  
        out.write(response);
        return found;
     }
docs: Convinience routine for selecting SOCKSv5 authentication.
  <p>
  This method reads in authentication methods that client supports,
  checks wether it supports given method. If it does, the notification
  method is written back to client, that this method have been chosen
  for authentication. If given method was not found, authentication
  failure message is send to client ([5,FF]).
  @param in Input stream, version byte should be removed from the stream
  before calling this method.
  @param out Output stream.
  @param methodId Method which should be selected.
  @return true if methodId was found, false otherwise.