Improved onClose handler, tracks whether the socket was closed before the handler got attached.