anchor.diff:  	       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})
  +           undefined -> ok;
  +           _ ->  lists:foreach(fun(Link) -> 
  +                   bind_refs(Link, State)
  +                 end, Links),
  +                 gen_server:cast(Protocol, {send, Payload})
   	       end
       end
     catch
  
positive.code:          terminate/2, code_change/3]).
  
  -record(state, {channel, protocol, subscriptions=[], exchanges=[], queues=[], sessionid, api_key}).
  
  -include_lib("amqp_client/include/amqp_client.hrl").
  -include_lib("gateway.hrl").
  
negative.code: code_change(_OldVsn, State, _Extra) ->
    {ok, State}.
  
  
  %% RabbitMQ command generators
  
  get_resource(client_exchange, SessionId, _ApiKey) ->
    #bridge_resource{
              name = SessionId,
              rabbit_name = list_to_binary(["T_", SessionId]),
              bridge_type = client_exchange,
              rabbit_type = {exchange, <<"topic">>}};
  
  get_resource(client_queue, SessionId, _ApiKey) ->
    #bridge_resource{
              name = SessionId,
              rabbit_name = list_to_binary(["C_", SessionId]),
              bridge_type = client_queue,
              rabbit_type = queue};
  
  get_resource(namespace, Namespace, _ApiKey) ->
    #bridge_resource{
              name = Namespace,
              rabbit_name = list_to_binary(["T_", Namespace]),
              bridge_type = namespace,
              rabbit_type = {exchange, <<"topic">>}};
  
  get_resource(service, Service, ApiKey) ->
    #bridge_resource{
              name = Service,
              rabbit_name = list_to_binary([<<"W_">>, Service, <<"_">>, ApiKey]),
              bridge_type = service,
              rabbit_type = queue};
  
  get_resource(channel, Channel, ApiKey) ->
    #bridge_resource{
              name = Channel,
              rabbit_name = list_to_binary([<<"F_">>, Channel, <<"_">>, ApiKey]),
              bridge_type = channel,
              rabbit_type = {exchange, <<"fanout">>}}.
  
  declare(ResourceType, ResourceName, ApiKey, AMQPChannel) ->
    Resource = get_resource(ResourceType, ResourceName, ApiKey),
    case Resource of
      #bridge_resource{rabbit_name = Name, rabbit_type = queue} ->
        QueueDeclare = #'queue.declare'{queue = Name},
        #'queue.declare_ok'{queue = QueueName} = amqp_channel:call(AMQPChannel, QueueDeclare);
  
      #bridge_resource{rabbit_name = Name, rabbit_type = {exchange, ExchangeType}} ->
        ExchangeDeclare = #'exchange.declare'{exchange = Name, type = ExchangeType},
        #'exchange.declare_ok'{} = amqp_channel:call(AMQPChannel, ExchangeDeclare)
  
    end,
    {ok, Resource}.
  
  get_routing_key(_Sink = #bridge_resource{bridge_type = namespace, name = Name}, ApiKey) ->
    list_to_binary([ApiKey, <<".named.#">>]);
  
  get_routing_key(#bridge_resource{bridge_type = client_queue, name = SessionId}, ApiKey) ->
    list_to_binary( [ApiKey, <<".client.">>, SessionId, <<".#">>] );
  
  get_routing_key(#bridge_resource{bridge_type = service, name = Name}, ApiKey) ->
    list_to_binary([ApiKey, <<".named.">>, Name, <<".#">>]);
  
  get_routing_key(#bridge_resource{bridge_type = channel, name = Name}, ApiKey) ->
    list_to_binary([ApiKey, <<".channel.">>, Name, <<".#">>]);
  
  get_routing_key(_, _) ->
    <<"">>.
  
  bind(Source = #bridge_resource{rabbit_type = {exchange, _}},
       Sink = #bridge_resource{rabbit_type = queue}, ApiKey, Channel
      ) ->
    QueueBinding = #'queue.bind'{queue = Sink#bridge_resource.rabbit_name,
                                 exchange = Source#bridge_resource.rabbit_name,
                                 routing_key = get_routing_key(Sink, ApiKey)},
    #'queue.bind_ok'{} = amqp_channel:call(Channel, QueueBinding),
    ok;
  
  bind(Source = #bridge_resource{rabbit_type = {exchange, _}},
       Sink = #bridge_resource{rabbit_type = {exchange, _}}, ApiKey, Channel
      ) ->
    ExchangeBinding = #'exchange.bind'{source      = Source#bridge_resource.rabbit_name,
                               destination = Sink#bridge_resource.rabbit_name,
                               routing_key = get_routing_key(Sink, ApiKey)},
    #'exchange.bind_ok'{} = amqp_channel:call(Channel, ExchangeBinding),
    ok.
  
  unbind(Source = #bridge_resource{rabbit_type = {exchange, _}},
         Sink = #bridge_resource{rabbit_type = queue}, ApiKey, Channel
        ) ->
    QueueBinding = #'queue.unbind'{queue = Sink#bridge_resource.rabbit_name,
                                 exchange = Source#bridge_resource.rabbit_name,
                                 routing_key = get_routing_key(Sink, ApiKey)},
    #'queue.unbind_ok'{} = amqp_channel:call(Channel, QueueBinding),
    ok;
  
  unbind(Source = #bridge_resource{rabbit_type = {exchange, _}},
         Sink = #bridge_resource{rabbit_type = {exchange, _}},ApiKey, Channel
        ) ->
    ExchangeBinding = #'exchange.unbind'{source      = Source#bridge_resource.rabbit_name,
                               destination = Sink#bridge_resource.rabbit_name,
                               routing_key = get_routing_key(Sink, ApiKey)},
    #'exchange.unbind_ok'{} = amqp_channel:call(Channel, ExchangeBinding),
    ok.
  
  
  consume_resource(#bridge_resource{rabbit_type = queue, rabbit_name = Name}, Channel) ->
    Sub = #'basic.consume'{queue = Name, no_ack = true},
    #'basic.consume_ok'{consumer_tag = Tag} = amqp_channel:subscribe(Channel, Sub, self()),
    {ok, Tag}.
  