    # File lib/active_record/vendor/mysql.rb, line 103
      def real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=nil)
        @server_status = SERVER_STATUS_AUTOCOMMIT
        if (host == nil or host == "localhost") and defined? UNIXSocket then
          unix_socket = socket || ENV["MYSQL_UNIX_PORT"] || MYSQL_UNIX_ADDR
          sock = UNIXSocket::new(unix_socket)
          @host_info = Error::err(Error::CR_LOCALHOST_CONNECTION)
          @unix_socket = unix_socket
        else
          sock = TCPSocket::new(host, port||ENV["MYSQL_TCP_PORT"]||(Socket::getservbyname("mysql","tcp") rescue MYSQL_PORT))
          @host_info = sprintf Error::err(Error::CR_TCP_CONNECTION), host
        end
        @host = host ? host.dup : nil
        sock.setsockopt Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true
        @net = Net::new sock

        a = read
        @protocol_version = a.slice!(0)
        @server_version, a = a.split(/\0/,2)
        @thread_id, @scramble_buff = a.slice!(0,13).unpack("La8")
        if a.size >= 2 then
          @server_capabilities, = a.slice!(0,2).unpack("v")
        end
        if a.size >= 16 then
          @server_language, @server_status = a.slice!(0,3).unpack("cv")
        end

        flag = 0 if flag == nil
        flag |= @client_flag | CLIENT_CAPABILITIES
        flag |= CLIENT_CONNECT_WITH_DB if db

        @pre_411 = (0 == @server_capabilities & PROTO_AUTH41)
        if @pre_411
          data = Net::int2str(flag)+Net::int3str(@max_allowed_packet)+
                 (user||"")+"\0"+
                       scramble(passwd, @scramble_buff, @protocol_version==9)
        else
          dummy, @salt2 = a.unpack("a13a12")
          @scramble_buff += @salt2
          flag |= PROTO_AUTH41
          data = Net::int4str(flag) + Net::int4str(@max_allowed_packet) +
                 ([8] + Array.new(23, 0)).pack("c24") + (user||"")+"\0"+
                 scramble41(passwd, @scramble_buff)
        end

        if db and @server_capabilities & CLIENT_CONNECT_WITH_DB != 0
          data << "\0" if @pre_411
          data << db
          @db = db.dup
        end
        write data
        pkt = read
        handle_auth_fallback(pkt, passwd)
        ObjectSpace.define_finalizer(self, Mysql.finalizer(@net))
        self
      end