diff --git a/lib/socket.js b/lib/socket.js
index <HASH>..<HASH> 100644
--- a/lib/socket.js
+++ b/lib/socket.js
@@ -560,11 +560,11 @@ Socket.prototype.onClose = function (reason, desc) {
     }, 0);
     var prev = this.readyState;
     this.readyState = 'closed';
+    this.id = null;
     if (prev == 'open') {
       this.emit('close', reason, desc);
+      this.onclose && this.onclose.call(this);
     }
-    this.onclose && this.onclose.call(this);
-    this.id = null;
   }
 };
socket: don't call `socket#onclose` if previous state was not `open`