anchor.diff:    Channel = gen_server:call(gateway_gamqp, get_new_channel),
     {ok, #state{channel=Channel}}.
   
  -handle_call({ready, Protocol, SessionId, ApiKey}, _From,
  -    State = #state{
  -      channel = Channel, queues=Queues, exchanges=Exchanges
  -    }
  -  ) ->
  -  ExchangeName = list_to_binary(["T_", SessionId]),
  -  ExchangeDeclare = #'exchange.declare'{exchange = ExchangeName,
  -                                        type = <<"topic">>
  -                                        % , arguments = [{"alternate-exchange", longstr, "F_ERROR"}]
  -                                       },
  -  #'exchange.declare_ok'{} = amqp_channel:call(Channel, ExchangeDeclare),
  -
  -  QueueName = list_to_binary(["C_", SessionId]),
  -  QueueDeclare = #'queue.declare'{queue = QueueName},
  -  #'queue.declare_ok'{queue = QueueName} = amqp_channel:call(Channel, QueueDeclare),
  -
  -  ExchangeBinding = #'exchange.bind'{source      = ExchangeName,
  -                             destination = <<"T_NAMED">>,
  -                             routing_key = list_to_binary([ApiKey, <<".named.#">>])},
  -  #'exchange.bind_ok'{} = amqp_channel:call(Channel, ExchangeBinding),
  -
  -  QueueBinding = #'queue.bind'{exchange      = ExchangeName,
  -                               queue = list_to_binary(["C_", SessionId]),
  -                               routing_key = list_to_binary( [ApiKey, <<".client.">>, SessionId, <<".#">>] )},
  -  #'queue.bind_ok'{} = amqp_channel:call(Channel, QueueBinding),
  -
  +
  +handle_call({ready, Protocol, SessionId, ApiKey}, _From, State = #state{channel = Channel}) ->
  +  {ok, ClientExchange} = declare(client_exchange, SessionId, ApiKey, Channel),
  +  {ok, ClientQueue} = declare(client_queue, SessionId, ApiKey, Channel),
  +  Namespace = get_resource(namespace, <<"NAMED">>, ApiKey),
  +  bind(ClientExchange, Namespace, ApiKey, Channel),
  +  bind(ClientExchange, ClientQueue, ApiKey, Channel),
     amqp_channel:register_return_handler(Channel, self()),
   
  -  Sub = #'basic.consume'{queue = QueueName, no_ack = true},
  -  #'basic.consume_ok'{} = amqp_channel:subscribe(Channel, Sub, self()),
  +  {ok, Tag} = consume_resource(ClientQueue, Channel),
   
     {reply, ok,
       State#state{
         protocol=Protocol,
  -      queues=[QueueName|Queues],
  -      exchanges=[ExchangeName|Exchanges],
  +      queues=[ClientQueue|State#state.queues],
  +      exchanges=[ClientExchange|State#state.exchanges],
         sessionid=SessionId,
         api_key = ApiKey
       }
  
positive.code: 
  handle_cast({change_protocol, Protocol}, State) ->
    {noreply, State#state{protocol=Protocol}};
  handle_cast({join_workerpool, Name}, State = #state{channel = Channel, subscriptions=Subscriptions, api_key = ApiKey}) ->
    QueueName = list_to_binary([<<"W_">>, Name, <<"_">>, ApiKey]),
    QueueDeclare = #'queue.declare'{queue = QueueName},
    #'queue.declare_ok'{queue = QueueName} = amqp_channel:call(Channel, QueueDeclare),
    QueueBinding = #'queue.bind'{queue = QueueName,
                                 exchange = <<"T_NAMED">>,
                                 routing_key = list_to_binary([ApiKey, <<".named.">>, Name, <<".#">>])},
    #'queue.bind_ok'{} = amqp_channel:call(Channel, QueueBinding),
  
    Sub = #'basic.consume'{queue = QueueName, no_ack = true},
    #'basic.consume_ok'{consumer_tag = Tag} = amqp_channel:subscribe(Channel, Sub, self()),
    {noreply, State#state{subscriptions=[{QueueName, Tag}|Subscriptions]}};
  handle_cast({leave_workerpool, Name}, State = #state{channel = Channel, subscriptions=Subscriptions, api_key = ApiKey, protocol = Protocol}) ->
    QueueName = list_to_binary([<<"W_">>, Name, <<"_">>, ApiKey]),
    NewSubs = case lists:keytake(QueueName, 1, Subscriptions) of
      {value, {QueueName, Tag}, NewList} -> amqp_channel:call(Channel, #'basic.cancel'{consumer_tag = Tag}),
                                            NewList;
                                   false -> gen_server:cast(Protocol, {error, 115, ["Cannot unpublish service that was not published", Name]}),
                                            Subscriptions
    end,
    {noreply, State#state{subscriptions=NewSubs}};
  handle_cast({join_channel, Name, HandlerSessionId, Writeable}, State = #state{channel = Channel, api_key = ApiKey}) ->
    ChannelExchange = list_to_binary([<<"F_">>, Name, <<"_">>, ApiKey]),
    ExchangeDeclare = #'exchange.declare'{exchange = ChannelExchange,
                                    type = <<"fanout">>
                                    %, arguments = [{"alternate-exchange", longstr, "F_ERROR"}]
                                  },
    #'exchange.declare_ok'{} = amqp_channel:call(Channel, ExchangeDeclare),
    if 
      Writeable ->
        ExchangeBinding = #'exchange.bind'{source  = list_to_binary([<<"T_">>, HandlerSessionId]),
  					 destination = ChannelExchange,
  					 routing_key = list_to_binary([ApiKey, <<".channel.">>, Name, <<".#">>])},
        #'exchange.bind_ok'{} = amqp_channel:call(Channel, ExchangeBinding);
      true -> ok
    end,
    QueueBinding = #'queue.bind'{queue = list_to_binary(["C_", HandlerSessionId]),
                                 exchange = ChannelExchange},
                                 % routing_key = <<"#">>},
    #'queue.bind_ok'{} = amqp_channel:call(Channel, QueueBinding),
    {noreply, State};
  handle_cast({get_channel, Name, HandlerSessionId}, State = #state{channel = Channel, api_key = ApiKey}) ->
    ChannelExchange = list_to_binary([<<"F_">>, Name, <<"_">>, ApiKey]),
    ExchangeDeclare = #'exchange.declare'{exchange = ChannelExchange,
                                    type = <<"fanout">>
                                    %, arguments = [{"alternate-exchange", longstr, "F_ERROR"}]
                                  },
    #'exchange.declare_ok'{} = amqp_channel:call(Channel, ExchangeDeclare),
    ExchangeBinding = #'exchange.bind'{source  = list_to_binary([<<"T_">>, HandlerSessionId]),
                                   destination = ChannelExchange,
                                   routing_key = list_to_binary([ApiKey, <<".channel.">>, Name, <<".#">>])},
    #'exchange.bind_ok'{} = amqp_channel:call(Channel, ExchangeBinding),
    {noreply, State};
  handle_cast({leave_channel, Name, HandlerSessionId}, State = #state{channel = Channel, api_key = ApiKey}) ->
    ChannelExchange = list_to_binary([<<"F_">>, Name, <<"_">>, ApiKey]),
    ExchangeBinding = #'exchange.unbind'{source  = list_to_binary(["T_", HandlerSessionId]),
                                   destination = ChannelExchange,
                                   routing_key = list_to_binary([ApiKey, <<".channel.">>, Name, <<".#">>])},
    #'exchange.unbind_ok'{} = amqp_channel:call(Channel, ExchangeBinding),
    QueueBinding = #'queue.unbind'{queue = list_to_binary(["C_", HandlerSessionId]),
                                 exchange = ChannelExchange},
                                 % routing_key = <<"#">>},
    #'queue.unbind_ok'{} = amqp_channel:call(Channel, QueueBinding),
    {noreply, State};
  
  handle_cast({publish_message, SessionId, Message}, State = #state{channel = Channel, api_key = ApiKey, protocol = Protocol}) ->
    Destination = proplists:get_value(<<"destination">>, Message),
  
    case gateway_util:extract_pathstring_from_nowref(Destination) of
  
negative.code:          terminate/2, code_change/3]).
  
  -record(state, {channel, protocol, subscriptions=[], exchanges=[], queues=[], sessionid, api_key}).
  -record(bridge_resource, {name, rabbit_name, rabbit_type, bridge_type}).
  
  -include_lib("amqp_client/include/amqp_client.hrl").
  -include_lib("gateway.hrl").
  