def connect!
      info "connecting to master at #{@api_uri}"
      headers = {
          'Kontena-Node-Id' => @node_id.to_s,
          'Kontena-Node-Name' => @node_name,
          'Kontena-Version' => Kontena::Agent::VERSION,
          'Kontena-Node-Labels' => @node_labels.join(','),
          'Kontena-Connected-At' => Time.now.utc.strftime(STRFTIME),
      }

      if @node_token
        headers['Kontena-Node-Token'] = @node_token.to_s
      elsif @grid_token
        headers['Kontena-Grid-Token'] = @grid_token.to_s
      else
        fail "Missing grid, node token"
      end

      @ws = Kontena::Websocket::Client.new(@api_uri,
        headers: headers,
        ssl_params: @ssl_params,
        ssl_hostname: @ssl_hostname,
        connect_timeout: CONNECT_TIMEOUT,
        open_timeout: OPEN_TIMEOUT,
        ping_interval: PING_INTERVAL,
        ping_timeout: PING_TIMEOUT,
        close_timeout: CLOSE_TIMEOUT,
      )

      async.connect_client @ws

      publish('websocket:connect', nil)

    rescue => exc
      error exc
      reconnect!
    end
def connect!
      info "connecting to master at #{@api_uri}"
      headers = {
          'Kontena-Node-Id' => @node_id.to_s,
          'Kontena-Node-Name' => @node_name,
          'Kontena-Version' => Kontena::Agent::VERSION,
          'Kontena-Node-Labels' => @node_labels.join(','),
          'Kontena-Connected-At' => Time.now.utc.strftime(STRFTIME),
      }

      if @node_token
        headers['Kontena-Node-Token'] = @node_token.to_s
      elsif @grid_token
        headers['Kontena-Grid-Token'] = @grid_token.to_s
      else
        fail "Missing grid, node token"
      end

      @ws = Kontena::Websocket::Client.new(@api_uri,
        headers: headers,
        ssl_params: @ssl_params,
        ssl_hostname: @ssl_hostname,
        connect_timeout: CONNECT_TIMEOUT,
        open_timeout: OPEN_TIMEOUT,
        ping_interval: PING_INTERVAL,
        ping_timeout: PING_TIMEOUT,
        close_timeout: CLOSE_TIMEOUT,
      )

      async.connect_client @ws

      publish('websocket:connect', nil)

    rescue => exc
      error exc
      reconnect!
    end
Connect to server, and start connect_client task

 Calls reconnect! on errors