diff --git a/lib/socket.js b/lib/socket.js
index <HASH>..<HASH> 100644
--- a/lib/socket.js
+++ b/lib/socket.js
@@ -586,12 +586,22 @@ Socket.prototype.close = function () {
       close();
     }
 
+    function waitForUpgrade() {
+      // wait for upgrade to finish since we can't send packets while pausing a transport
+      self.once('upgrade', cleanupAndClose);
+      self.once('upgradeError', cleanupAndClose);
+    }
+
     if (this.writeBuffer.length) {
-      this.once('drain', close);
+      this.once('drain', function() {
+        if (this.upgrading) {
+          waitForUpgrade();
+        } else {
+          close();
+        }
+      });
     } else if (this.upgrading) {
-      // wait for upgrade to finish since we can't send packets while pausing a transport
-      this.once('upgrade', cleanupAndClose);
-      this.once('upgradeError', cleanupAndClose);
+      waitForUpgrade();
     } else {
       close();
     }
Fixed transport close deferring logic.

Transport can still be upgrading after deferring until the drain event.