# File lib/socksify.rb, line 184
  def socks_authenticate
    if self.class.socks_username || self.class.socks_password
      Socksify::debug_debug "Sending username/password authentication"
      write "\005\001\002"
    else
      Socksify::debug_debug "Sending no authentication"
      write "\005\001\000"
    end
    Socksify::debug_debug "Waiting for authentication reply"
    auth_reply = recv(2)
    if auth_reply.empty?
      raise SOCKSError.new("Server doesn't reply authentication")
    end
    if auth_reply[0..0] != "\004" and auth_reply[0..0] != "\005"
      raise SOCKSError.new("SOCKS version #{auth_reply[0..0]} not supported")
    end
    if self.class.socks_username || self.class.socks_password
      if auth_reply[1..1] != "\002"
        raise SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
      end
      auth = "\001"
      auth += self.class.socks_username.to_s.length.chr
      auth += self.class.socks_username.to_s
      auth += self.class.socks_password.to_s.length.chr
      auth += self.class.socks_password.to_s
      write auth
      auth_reply = recv(2)
      if auth_reply[1..1] != "\000"
        raise SOCKSError.new("SOCKS authentication failed")
      end
    else
      if auth_reply[1..1] != "\000"
        raise SOCKSError.new("SOCKS authentication method #{auth_reply[1..1]} neither requested nor supported")
      end
    end
  end