anchor.diff:              S;
           #qos{ level = exactly_once,
                 message_id = Id } ->
  -            #state{ rpcs = RPC } = S,
               Ack = #pubrec{ message_id = Id },
               write(S, Ack),
  -            RPC1 = gb_trees:insert(?REMOTE(Id), Ack, RPC),
  -            S#state{ rpcs = RPC1 }
  +            S
       end.
   
   %% `recv` is used to synchronously get another frame from the
  
positive.code: %% This is step two of "exactly once" delivery (step one is publish),
  %% the first of two acknowledgments.
  opened({frame, #pubrec{ message_id = Id }}, S0) ->
      #state{ rpcs = RPC } = S0,
      #publish{} = gb_trees:get(?LOCAL(Id), RPC),
      Ack = #pubrel{ message_id = Id },
      %% NB update
      RPC1 = gb_trees:update(?LOCAL(Id), Ack, RPC),
      write(S0, Ack),
      ?OPENED(S0#state{ rpcs = RPC1 });
  
  %% This is step three of exactly once; we receive it if we send pubrec
  %% to acknowledge a publish with QoS = exactly_once
  opened({frame, #pubrel{ message_id = Id }}, S0) ->
      #state{ rpcs = RPC } = S0,
      #pubrec{} = gb_trees:get(?REMOTE(Id), RPC),
      RPC1 = gb_trees:delete(?REMOTE(Id), RPC),
      write(S0, #pubcomp{ message_id = Id }),
      ?OPENED(S0#state{ rpcs = RPC1 });
  
  %% These two ff frames end a "guaranteed delivery" exchange.
  opened({frame, #puback{ message_id = Id }}, S0) ->
      #state{ rpcs = RPC } = S0,
      #publish{} = gb_trees:get(?LOCAL(Id), RPC),
      RPC1 = gb_trees:delete(?LOCAL(Id), RPC),
      ?OPENED(S0#state{ rpcs = RPC1 });
  
  opened({frame, #pubcomp{ message_id = Id }}, S0) ->
      #state{ rpcs = RPC } = S0,
      #pubrel{} = gb_trees:get(?LOCAL(Id), RPC),
      RPC1 = gb_trees:delete(?LOCAL(Id), RPC),
      ?OPENED(S0#state{ rpcs = RPC1 });
  
  %% Unaccounted for thus far: #suback, #unsuback
  opened({frame, Frame}, S0) ->
      #state{ rpcs = RPC0 } = S0,
      {Id, Reply} = make_reply(Frame),
      {Ref, From} = gb_trees:get(?LOCAL(Id), RPC0),
      RPC1 = gb_trees:delete(?LOCAL(Id), RPC0),
      From ! {Ref, Reply},
      ?OPENED(S0#state{ rpcs = RPC1 });
  
  
negative.code: do_rpc(S0, Req, Ref, From) ->
      #state{ id_counter = Id,
              rpcs = RPC } = S0,
      RPC1 = gb_trees:insert(Id, {Ref, From}, RPC),
      write(S0, with_id(Id, Req)),
      S0#state{ id_counter = Id + 1, rpcs = RPC1 }.
  
  with_id(Id, S = #subscribe{})   -> S#subscribe{ message_id = Id };
  with_id(Id, U = #unsubscribe{}) -> U#unsubscribe{ message_id = Id }.
  
  dup_of(F = #publish{}) ->
      F#publish{ dup = true };
  dup_of(F = #subscribe{}) ->
      F#subscribe{ dup = true };
  dup_of(F = #pubrel{}) ->
      F#pubrel{ dup = true }.
  
  make_reply(#suback{ message_id = Id, qoses = QoSes }) ->
      {Id, {suback, QoSes}};
  make_reply(#unsuback{ message_id = Id }) ->
      {Id, unusback}.
  
  %% Send an acknowledgment.
  do_ack(S, #publish{ qos = QoS }) ->
      case QoS of
          at_most_once ->
  