 					connection.taskDidFinish (data: data, response: response, error: error, completion: completion);
 				};
 			});
-			self.taskPointer = Int (bitPattern: Unmanaged.passUnretained (task).toOpaque ());
+			self.taskPointer = UnsafePointer (Unmanaged.passUnretained (task).toOpaque ().assumingMemoryBound (to: URLSessionTask.self));
 			return task;
+		}
+		
+		fileprivate mutating func invalidateTask () {
+			self.taskPointer = nil;
 		}
 	}
 }

		fileprivate let request: Request;
		
		fileprivate var task: URLSessionTask {
			guard self.taskPointer != 0 else {
				log.fault ("URL session task is not created yet");
				return URLSessionTask ();
			}
			return unsafeBitCast (self.taskPointer, to: URLSessionTask.self);
		}
		
		private unowned let session: URLSession;
		private var taskPointer = 0;
		
		fileprivate init (session: URLSession, request: Request) {
			self.session = session;

import Foundation

public protocol KBAPIConnectionProtocol: AnyObject {
	var isCancelled: Bool { get }
	
	func cancel ();
}

