     _socket!.connect()
   }
   
+  /// Is the websocket actively connected?
+  var isConnected : Bool { get { return _socket?.isConnected ?? false } }
+  
   func websocketDidConnect(socket: WebSocket)
   {
     print("Connected to socket!")
     _connectionTime = Clock.createTimer()
+    // Reset the subscriptions
+    _subscriptions = [:]
     // Form an initial subscription packet
     var apiParts : [String] = ["\"rate\": 0"]
     _pendingSubscriptions.append("p.paused")
     if _pendingSubscriptions.count > 0 {
       let list = _pendingSubscriptions.map({"\"" + $0 + "\""}).joinWithSeparator(",")
       apiParts.append("\"+\": [\(list)]")
+      for name in _pendingSubscriptions {
+        let val = _subscriptions[name] ?? 0
+        _subscriptions[name] = val + 1
+      }
       _pendingSubscriptions.removeAll()
     }
     if _pendingOneshots.count > 0 {

    get { return _url }
  }
  
  var isConnected : Bool { get { return _socket?.isConnected ?? false } }
  
  private var _socket : WebSocket? = nil
  /// Stores the last data packet recieved
  private var _textMessage : String? = nil

  private var _latestData : [String: (time: Double, data: JSON)] = [:]
  private var _pendingSubscriptions : [String] = []
  private var _pendingOneshots : [String] = []
  
  /// Temporary subscriptions, that we only need the data once
  private var _temporarySubscriptions = Set<String>()
  
  private var _connectionTime : Timer?
  private var _dumpTimer : Timer?
  
  private var _subscriptions : [String : Int ] = [:]
  private var _parseQueue : dispatch_queue_t
  private var dropped : Int = 0
  private var reading : Int = 0
  private var errored = Set<String>()
  
  init (hostname : String, port : UInt) throws {


/// Stores result values sent from Kerbal
protocol IKerbalDataStore {
  /// Retrieve a single data value
  subscript(index : String) -> JSON? { get }
  
  /** Finds the age of the named data variable.
  - parameter name The data variable to query
  - returns: Number of seconds since update,
             or infinity if unknown */
  func ageOfData(name : String) -> Double
  
  /// Subscribe to a single API variable
  func subscribe(name : String)
  /// Unsubscribe from a single API variable
  func unsubscribe(name : String)
  /// Subscribe to a whole number of API variables
  func subscribe(apiNames : [String])
  /// Unsubscribe from a whole number of API variables
  func unsubscribe(apiNames : [String])
  /// Send an API request to the server only once
  func oneshot(name : String)
  
  /** 
