
const debug = require('debug')('http-close')

module.exports = function httpClose(options, server) {
  if (!server) {
    server = options
    options = {}
  }

  const sockets = []
  const timeout = options.timeout || 5000

  server.on('connection', function (socket) {
    sockets.push(socket)
    socket.on('close', function () {
      sockets.splice(sockets.indexOf(socket), 1)
    })
  })

  const close = server.close
  server.close = function () {
    debug('server close')

    try {
      close.apply(this, arguments)
    } catch (err) {
      if (err.message !== 'Not running') {
        throw err
      }
    }

    sockets.forEach(function (socket) {
      const res = socket._httpMessage

      // Close all keep-alive sockets
      if (!res) {
        socket.destroy()
        return
      }

      // If headers is not sent, then say we don't want keep-alive
      if (!res.headersSent) {
        res.setHeader('Connection', 'close')
      }

      if (timeout) {
        socket.setTimeout(timeout)
      }
    })

    sockets.length = 0

    if (timeout) {
      server.on('timeout', function (socket) {
        debug('socket timeout')

        const res = socket._httpMessage

        if (!res) {
          debug('no ServerResponse')
          socket.destroy()
          return
        }

        if (res.headersSent) {
          debug('no response in time')
          socket.destroy()
          return
        }

        debug('write 500')
        res.writeHead(500, { Connection: 'close'})
        res.end()
      })

    } // if (timeout)

  } // server.close
}

@@ -177,10 +177,17 @@
 s = 
-%5B%5D
+new Set()
 %0A  c
@@ -226,16 +226,75 @@
 %7C 5000%0A%0A
+  function onSocketClose() %7B%0A    sockets.delete(this)%0A  %7D%0A%0A
   server
@@ -347,12 +347,11 @@
 ets.
-push
+add
 (soc
@@ -382,76 +382,21 @@
 e', 
-function () %7B%0A      sockets.splice(sockets.indexOf(socket), 1)%0A    %7D
+onSocketClose
 )%0A  
@@ -1050,18 +1050,15 @@
 ets.
+c
 le
-ngth = 0
+ar()
 %0A%0A  

use `Set` instead of an array for keeping the socket refs