# File lib/mcollective/connector/activemq.rb, line 215
      def connect(connector = ::Stomp::Connection)
        if @connection
          Log.debug("Already connection, not re-initializing connection")
          return
        end

        begin
          @base64 = get_bool_option("activemq.base64", "false")
          @msgpriority = get_option("activemq.priority", 0).to_i

          pools = Integer(get_option("activemq.pool.size"))
          hosts = []
          middleware_user = ''
          middleware_password = ''
          prompt_for_username = get_bool_option("activemq.prompt_user", "false")
          prompt_for_password = get_bool_option("activemq.prompt_password", "false")
          
          if prompt_for_username
            Log.debug("No previous user exists and activemq.prompt-user is set to true")
            print "Please enter user to connect to middleware: "
            middleware_user = STDIN.gets.chomp
          end

          if prompt_for_password
            Log.debug("No previous password exists and activemq.prompt-password is set to true")
            middleware_password = MCollective::Util.get_hidden_input("Please enter password: ")
            print "\n"
          end

          1.upto(pools) do |poolnum|
            host = {}

            host[:host] = get_option("activemq.pool.#{poolnum}.host")
            host[:port] = get_option("activemq.pool.#{poolnum}.port", 61613).to_i
            host[:ssl] = get_bool_option("activemq.pool.#{poolnum}.ssl", "false")
            
            # read user from config file
            host[:login] = get_env_or_option("STOMP_USER", "activemq.pool.#{poolnum}.user", middleware_user)
            if prompt_for_username and host[:login] != middleware_user
                Log.info("Using #{host[:login]} from config file to connect to #{host[:host]}. "+
                        "plugin.activemq.prompt_user should be set to false to remove the prompt.")
            end
            
            # read user from config file
            host[:passcode] = get_env_or_option("STOMP_PASSWORD", "activemq.pool.#{poolnum}.password", middleware_password)
            if prompt_for_password and host[:passcode] != middleware_password
                Log.info("Using password from config file to connect to #{host[:host]}. "+
                        "plugin.activemq.prompt_password should be set to false to remove the prompt.")
            end

            # if ssl is enabled set :ssl to the hash of parameters
            if host[:ssl]
              host[:ssl] = ssl_parameters(poolnum, get_bool_option("activemq.pool.#{poolnum}.ssl.fallback", "false"))
            end

            Log.debug("Adding #{host[:host]}:#{host[:port]} to the connection pool")
            hosts << host
          end

          raise "No hosts found for the ActiveMQ connection pool" if hosts.size == 0

          connection = {:hosts => hosts}

          # Various STOMP gem options, defaults here matches defaults for 1.1.6 the meaning of
          # these can be guessed, the documentation isn't clear
          connection[:use_exponential_back_off] = @use_exponential_back_off
          connection[:initial_reconnect_delay] = @initial_reconnect_delay
          connection[:back_off_multiplier] = @back_off_multiplier
          connection[:max_reconnect_delay] = @max_reconnect_delay
          connection[:max_reconnect_attempts] = Integer(get_option("activemq.max_reconnect_attempts", 0))
          connection[:randomize] = get_bool_option("activemq.randomize", "false")
          connection[:backup] = get_bool_option("activemq.backup", "false")
          connection[:timeout] = Integer(get_option("activemq.timeout", -1))
          connection[:connect_timeout] = Integer(get_option("activemq.connect_timeout", 30))
          connection[:reliable] = true
          connection[:connect_headers] = connection_headers
          connection[:max_hbrlck_fails] = Integer(get_option("activemq.max_hbrlck_fails", 0))
          connection[:max_hbread_fails] = Integer(get_option("activemq.max_hbread_fails", 2))

          connection[:logger] = EventLogger.new

          @connection = connector.new(connection)

        rescue ClientTimeoutError => e
          raise e
        rescue Exception => e
          raise("Could not connect to ActiveMQ Server: #{e}")
        end
      end