# File lib/active_record/vendor/mysql411.rb, line 38
  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 || host == "localhost" ) && defined? UNIXSocket
      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)

    # Store the version number components for speedy comparison
    version, ostag = @server_version.split( /-/, 2 )
    @major_ver, @minor_ver, @revision_num = version.split( /\./ ).map { |v| v.to_i }
    
    @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.unpack("cv")
    end
    
    # Set the flags we'll send back to the server
    flag = 0 if flag == nil
    flag |= @client_flag | CLIENT_CAPABILITIES
    flag |= CLIENT_CONNECT_WITH_DB if db
    
    if version_meets_minimum?( 4, 1, 1 )
      # In 4.1.1+ the seed comes in two parts which must be combined
      a.slice!( 0, 16 )
      seed_part_2 = a.slice!( 0, 12 );      
      @scramble_buff << seed_part_2
      
      flag |= CLIENT_FOUND_ROWS
      flag |= CLIENT_PROTOCOL_41
      flag |= CLIENT_SECURE_CONNECTION if @server_capabilities & CLIENT_SECURE_CONNECTION;
      
      if db && @server_capabilities & CLIENT_CONNECT_WITH_DB != 0
        @db = db.dup
      end
      
      scrambled_password = scramble411( passwd, @scramble_buff, @protocol_version==9 )
      data = make_client_auth_packet_41( flag, user, scrambled_password, db )
    else
      scrambled_password = scramble( passwd, @scramble_buff, @protocol_version == 9 )
      data = Net::int2str(flag)+Net::int3str(@max_allowed_packet)+(user||"")+"\0"+scrambled_password
      if db and @server_capabilities & CLIENT_CONNECT_WITH_DB != 0 then
        data << "\0"+db
        @db = db.dup
      end
    end
    
    write data
    read
    self
  end