   )
 
 
-connectionRequest = (deviceId, callback) ->
-  # ensure socket is reused in case of digest authentication
+httpConReq = (url, username, password, timeout, callback) ->
+  options = URL.parse(url)
+  # Ensure socket is reused in case of digest authentication
   agent = new http.Agent({maxSockets : 1})
 
   statusToError = (statusCode) ->

URL = require 'url'
auth = require './auth'

udpConReq = (address, un, key, callback) ->
  [host, port] = address.split(':', 2)


      else
        new Error("Unexpected response code from device: #{statusCode}")

  request = http.get(options, (res) ->
    if res.statusCode == 401 and res.headers['www-authenticate']?
      authHeader = auth.parseAuthHeader(res.headers['www-authenticate'])
      if authHeader.method is 'Basic'
        options.headers = {'Authorization' : auth.basic(username, password)}
      else if authHeader.method is 'Digest'
        options.headers = {
          'Authorization' : auth.digest(username, password, options.path, 'GET', null, authHeader)
        }

      request = http.get(options, (res) ->
        if res.statusCode == 0
          # Workaround for some devices unexpectedly closing the connection
          request = http.get(options, (res) ->
            callback(statusToError(res.statusCode))
            res.resume()
          ).on('error', (err) ->
            request.abort()
            callback(statusToError(0))
          ).on('socket', (socket) ->
            socket.setTimeout(timeout)
            socket.on('timeout', () -> request.abort())
          )
        else
          callback(statusToError(res.statusCode))
        res.resume()
      ).on('error', (err) ->
        request.abort()
        callback(statusToError(0))
      ).on('socket', (socket) ->
        socket.setTimeout(timeout)
        socket.on('timeout', () -> request.abort())
      )
    else
      callback(statusToError(res.statusCode))

    # No listener for data so emit resume
    res.resume()
  ).on('error', (err) ->
    request.abort()
    callback(statusToError(0))
  ).on('socket', (socket) ->
    socket.setTimeout(timeout)
    socket.on('timeout', () -> request.abort())
  )


connectionRequest = (deviceId, callback) ->
  proj = {
    'Device.ManagementServer.ConnectionRequestURL._value' : 1,
    'Device.ManagementServer.UDPConnectionRequestAddress._value' : 1,
