     internal init(session: NSURLSession, host: String, port: Int) {
         self._underlyingSession = session
         if #available(iOS 9.0, OSX 10.11, *) {
-            self._underlyingTaskObject = session.streamTaskWithHostName(host, port: port)
-            self._taskIdentifier = self._underlyingTaskObject!.taskIdentifier
-            super.init()
+            let task = session.streamTaskWithHostName(host, port: port)
+            self._taskIdentifier = task.taskIdentifier
+            FPSStreamTask.streamTasks[_taskIdentifier] = task
         } else {
             lasttaskIdAssociated += 1
             self._taskIdentifier = lasttaskIdAssociated
             self.host = (host, port)
             self.dispatch_queue = dispatch_queue_create("FSPStreamTask", DISPATCH_QUEUE_CONCURRENT)
-            super.init()
         }
     }
     
     internal init(session: NSURLSession, netService: NSNetService) {
         self._underlyingSession = session
         if #available(iOS 9.0, OSX 10.11, *) {
-            self._underlyingTaskObject = session.streamTaskWithNetService(netService)
-            self._taskIdentifier = self._underlyingTaskObject!.taskIdentifier
+            let task = session.streamTaskWithNetService(netService)
+            self._taskIdentifier = task.taskIdentifier
+            FPSStreamTask.streamTasks[_taskIdentifier] = task
         } else {
             lasttaskIdAssociated += 1
             self._taskIdentifier = lasttaskIdAssociated

    private var streamDelegate: FPSStreamDelegate? {
        return (_underlyingSession.delegate as? FPSStreamDelegate)
    }
    private var _underlyingTaskObject: NSURLSessionTask?
    private var _taskIdentifier: Int
    
    @available(iOS 9.0, OSX 10.11, *)
    internal var _underlyingTask: NSURLSessionStreamTask? {
        get {
            return _underlyingTaskObject as? NSURLSessionStreamTask
        }
        set {
            _underlyingTaskObject = newValue
        }
    }
    
    public override var taskIdentifier: Int {

            guard let outputStream = outputStream, let inputStream = inputStream else {
                return
            }
            dispatch_async(dispatch_queue) {
                self.write(false)
                while inputStream.streamStatus != .AtEnd {
                    NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.1));
                    NSThread.sleepForTimeInterval(0.1)
