# File jabber4r/session.rb, line 258

    def authenticate(username, password, resource, digest=false)

      @username = username

      @password = password

      @resource = resource

      @jid = JID.new("#{username}@#{@host}/#{resource}")

      @roster.add(@jid, "both", "Me", "My Resources")

      

      msg_id = self.id

      authHandler = Proc.new  do |element| 

        if element.element_tag=="iq" and element.attr_id==msg_id

          element.consume_element

          if element.attr_type=="result"

            @authenticated = true

          elsif element.attr_type=="error"

            @authenticated = false

          end

        end

      end

      if digest

        require 'digest/sha1'

        authRequest = Jabber::Protocol::Iq.gen_auth_digest(self, msg_id, username, Digest::SHA1.new(@session_id + password).hexdigest, resource)

      else

        authRequest = Jabber::Protocol::Iq.gen_auth(self, msg_id, username, password, resource)

      end

      @connection.send(authRequest, authHandler)

      Thread.stop

      return @authenticated

    end