       else
         new Error("Unexpected response code from device: #{statusCode}")
 
-  conReq = (url, authString, callback) ->
-    options = URL.parse(url)
-    options.agent = agent
-
-    if authString
-      options.headers = {'Authorization' : authString}
-
-    request = http.get(options, (res) ->
-      if res.statusCode == 401 and res.headers['www-authenticate']?
-        authHeader = auth.parseAuthHeader(res.headers['www-authenticate'])
-      callback(res.statusCode, authHeader)
-      # don't need body, go ahead and emit events to free up the socket for possible reuse
-      res.resume()
-    )
-
-    request.on('error', (err) ->
-      # error event when request is aborted
-      request.abort()
-      callback(0)
-    )
-
-    request.on('socket', (socket) ->
-      socket.setTimeout(2000)
-      socket.on('timeout', () ->
+  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,

      username = device.InternetGatewayDevice.ManagementServer.ConnectionRequestUsername?._value
      password = device.InternetGatewayDevice.ManagementServer.ConnectionRequestPassword?._value

    if not (username and password) and config.auth?.connectionRequest
      [username, password] = config.auth.connectionRequest(deviceId)

    if udpConnectionRequestAddress
      udpConReq(udpConnectionRequestAddress, username, password, (err) -> throw err if err)

    # for testing
    #connectionRequestUrl = connectionRequestUrl.replace(/^(http:\/\/)([0-9\.]+)(\:[0-9]+\/[a-zA-Z0-9]+\/?$)/, '$110.1.1.254$3')
    conReq(connectionRequestUrl, null, (statusCode, authHeader) ->
      if statusCode == 401
        if authHeader.method is 'Basic'
          authString = auth.basic(username, password)
        else if authHeader.method is 'Digest'
          uri = URL.parse(connectionRequestUrl)
          authString = auth.digest(username, password, uri.path, 'GET', null, authHeader)

        conReq(connectionRequestUrl, authString, (statusCode, authHeader) ->
          if statusCode == 0
            # Workaround for some devices unexpectedly closing the connection
            return conReq(connectionRequestUrl, authString, (statusCode) -> callback(statusToError(statusCode)))
          callback(statusToError(statusCode))
        )
      else if udpConnectionRequestAddress
        return callback()
      else
        callback(statusToError(statusCode))
    )
  )



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


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

