anchor.diff:  
   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]),
  +
  +handle_cast({join_workerpool, Name}, 
  +            State = #state{channel = Channel,
  +                           subscriptions=Subscriptions,
  +                           api_key = ApiKey}) ->
  +  {ok, Service} = declare(service, Name, ApiKey, Channel),
  +  DefaultNamespace = get_resource(namespace, <<"NAMED">>, ApiKey),
  +  bind(Service, DefaultNamespace, Channel, ApiKey),
  +  {ok, Tag} = consume_resource(Service, Channel),
  +  {noreply, State#state{subscriptions=[{Service#bridge_resource.name, Tag}|Subscriptions]}};
  +
  +handle_cast({leave_workerpool, Name},  State = #state{subscriptions=Subscriptions}) ->
  +  QueueName = list_to_binary([<<"W_">>, Name, <<"_">>, State#state.api_key]),
     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
  +    {value, {QueueName, Tag}, NewList} -> 
  +       amqp_channel:call(State#state.channel, #'basic.cancel'{consumer_tag = Tag}),
  +       NewList;
  +     false -> 
  +       gen_server:cast(State#state.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);
  +
  +handle_cast({join_channel, Name, HandlerSessionId, Writeable},
  +             State = #state{channel = Channel, api_key = ApiKey}) ->
  + {ok, Chan} = declare(channel, Name, ApiKey, Channel),
  + ClientQueue = get_resource(client_queue, HandlerSessionId, ApiKey),
  + ClientExchange = get_resource(client_exchange, HandlerSessionId, ApiKey),
  + bind(Chan, ClientQueue, ApiKey, Channel),
  +  if
  +    Writeable -> bind(ClientExchange, Chan, ApiKey, Channel);
       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}) ->
  +  {noreply, State};
  +
  +handle_cast({get_channel, Name, HandlerSessionId},
  +            State = #state{channel = Channel, api_key = ApiKey}) ->
  +  {ok, Chan} = declare(channel, Name, ApiKey, Channel) ,
  +  ClientExchange =  get_resource(client_exchange, HandlerSessionId, ApiKey),
  +  bind(ClientExchange, Chan, ApiKey, Channel),
  +  {noreply, State};
  +
  +handle_cast({leave_channel, Name, HandlerSessionId},
  +            State = #state{channel = Channel, api_key = ApiKey}) ->
  +  ChannelExchange = get_resource(channel, Name, ApiKey),
  +  ClientExchange = get_resource(client_exchange, HandlerSessionId, ApiKey),
  +  ClientQueue = get_resource(client_queue, HandlerSessionId, ApiKey),
  +  unbind(ClientExchange, ChannelExchange, ApiKey, Channel),
  +  unbind(ChannelExchange, ClientQueue, ApiKey, Channel),
  +  {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
  
positive.code:   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),
  
    amqp_channel:register_return_handler(Channel, self()),
  
    Sub = #'basic.consume'{queue = QueueName, no_ack = true},
    #'basic.consume_ok'{} = amqp_channel:subscribe(Channel, Sub, self()),
  
    {reply, ok,
      State#state{
        protocol=Protocol,
        queues=[QueueName|Queues],
        exchanges=[ExchangeName|Exchanges],
        sessionid=SessionId,
        api_key = ApiKey
      }
  
negative.code: 	       gen_server:cast(self(), Cast);
         true -> Links = gateway_util:now_decode(Message),
  	       case Links of
             undefined -> ok;
             _ ->  lists:foreach(fun(Link) -> 
                     bind_refs(Link, State)
                   end, Links),
                   gen_server:cast(Protocol, {send, Payload})
  	       end
      end
    catch
  