     }
 
     private func connectWithToken() {
-        guard !isConnecting else {
+        guard !isConnecting && !isClosedByError else {
             return
         }
 
         guard retries < maxRetries else {
+            isClosedByError = true
             let domain = "swift.WebSocketManager"
             let description = "Invalid HTTP upgrade. Please verify your credentials."
             let error = createError(domain, description: description)

    }

    private func isAuthenticationFailure(error: NSError?) -> Bool {
        print("***** Authentication failure *****") // TODO: debugging
        guard let error = error,
              let description = error.userInfo[NSLocalizedDescriptionKey] as? String else
        {

                self.retries += 1
                self.connectWithToken()
            } else if let error = error {
                self.isClosedByError = true
                self.onError?(error)
            }
        }
