         }
         
         session = sessionProvider.createSession(for: self, lastEventID: lastEventID)
+        state = .connecting
         session.dataTask(with: url).resume()
-        state = .connecting
     }
     
     internal func isForcedClose(_ response: HTTPURLResponse?) -> Bool {

    }
    
    public func urlSession(_ session: URLSession, dataTask task: URLSessionDataTask, didReceive httpResponse: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
        guard !isForcedClose(httpResponse as? HTTPURLResponse) else {
            callback?.pyroEventSource(self, didReceiveError: PyroEventSourceError.forcedClose)
            close()
            return
        }
        
        if let responseError = response.isErroneous(httpResponse as? HTTPURLResponse) {
            callback?.pyroEventSource(self, didReceiveError: responseError)
            close()
            
        } else {
            completionHandler(.allow)
            state = .open
        }
    }
    
    public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        guard !isForcedClose(task.response as? HTTPURLResponse) else {
            callback?.pyroEventSource(self, didReceiveError: PyroEventSourceError.forcedClose)
            close()
            return
        }
        
        if let responseError = response.isErroneous(task.response as? HTTPURLResponse) {
            callback?.pyroEventSource(self, didReceiveError: responseError)
            close()
        
        } else {
            callback?.pyroEventSource(self, didReceiveError: error ?? RequestError.unknown)
        }
    }
}

    }
    
    public func close() {
        session?.invalidateAndCancel()
        session = nil
        state = .closed
    }
