 public typealias ChannelSubscriptionBlock = (NSDictionary) -> Void
 
 // MARK: FayeClientDelegate Protocol
-public protocol FayeClientDelegate: NSObjectProtocol{
+public protocol FayeClientDelegate: NSObjectProtocol {
   func messageReceived(client:FayeClient, messageDict: NSDictionary, channel: String)
   func connectedToServer(client:FayeClient)
   func disconnectedFromServer(client:FayeClient)



// MARK: BayuexChannel Messages
enum BayeuxChannel : CustomStringConvertible {
  case HANDSHAKE_CHANNEL;
  case CONNECT_CHANNEL;
  case DISCONNECT_CHANNEL;
  case SUBSCRIBE_CHANNEL;
  case UNSUBSCRIBE_CHANNEL;

  var description : String {
    switch self {
      // Use Internationalization, as appropriate.
    case .HANDSHAKE_CHANNEL: return "/meta/handshake";
    case .CONNECT_CHANNEL: return "/meta/connect";
    case .DISCONNECT_CHANNEL: return "/meta/disconnect";
    case .SUBSCRIBE_CHANNEL: return "/meta/subscribe";
    case .UNSUBSCRIBE_CHANNEL: return "/meta/unsubscribe";
    }
  }
}




// MARK: Private Bayuex Methods
private extension FayeClient {

  func parseFayeMessage(messageJSON:JSON) {
    let messageDict = messageJSON[0]
    if let channel = messageDict["channel"].string {

      // Handle Meta Channels
      if let metaChannel = BayeuxChannel(rawValue: channel) {
        switch(metaChannel) {
        case .HANDSHAKE_CHANNEL:
          self.fayeClientId = messageDict["clientId"].stringValue
          if messageDict["successful"].int == 1 {
            self.delegate?.connectedToServer(self)
            self.fayeConnected = true;
            self.connect()
            self.subscribeQueuedSubscriptions()

          } else {
            // OOPS
          }
        case .CONNECT_CHANNEL:
          if messageDict["successful"].int == 1 {
            self.fayeConnected = true;
            self.connect()
          } else {
            // OOPS
          }
        case .DISCONNECT_CHANNEL:
          if messageDict["successful"].int == 1 {
            self.fayeConnected = false;
            self.transport?.closeConnection()
            self.delegate?.disconnectedFromServer(self)
          } else {
            // OOPS
          }
        case .SUBSCRIBE_CHANNEL:
          if let success = messageJSON[0]["successful"].int where success == 1 {
            if let subscription = messageJSON[0]["subscription"].string {
              self.pendingSubscriptions.removeObject(subscription)
              self.openSubscriptions.addObject(subscription)
              self.delegate?.didSubscribeToChannel(self, channel: subscription)
            } else {
              print("Missing subscription for Subscribe")
            }
          } else {
            // Subscribe Failed
            if let error = messageJSON[0]["error"].string {
              self.delegate?.subscriptionFailedWithError(self, error: error)
            }
          }
        case .UNSUBSCRIBE_CHANNEL:
          if let subscription = messageJSON[0]["subscription"].string {
            self.openSubscriptions.removeObject(subscription)
            self.delegate?.didUnsubscribeFromChannel(self, channel: subscription)
          } else {
            print("Missing subscription for Unsubscribe")
          }
        }
      } else {
        // Handle Client Channel
        if self.isSubscribedToChannel(channel) {
          if messageJSON[0]["data"] != JSON.null {
            let data: AnyObject = messageJSON[0]["data"].object
            if let channelBlock = self.channelSubscriptionBlocks[channel] {
              channelBlock(data as! NSDictionary)
            } else {
              self.delegate?.messageReceived(self, messageDict: data as! NSDictionary, channel: channel)
            }
          } else {
            print("For some reason data is nil, maybe double posting?!")
          }
        } else {
          print("weird channel")
        }
      }
    } else {
      print("Missing channel")
    }
  }
