anchor.diff:      end.
   
   %% @private
  -handle_call({req, Req, _Timeout}, From, State) when State#state.req =/= undefined ->
  -    {noreply, queue_request(Req, undefined, From, State)};
  -handle_call({req, Req, _Timeout, Ctx}, From, State) when State#state.req =/= undefined ->
  -    {noreply, queue_request(Req, Ctx, From, State)};
  -handle_call({req, Req, _Timeout}, From, State) ->
  -    {noreply, send_request(Req, undefined, From, State)};
  -handle_call({req, Req, _Timeout, Ctx}, From, State) ->
  -    {noreply, send_request(Req, Ctx, From, State)}.
  +handle_call({req, Msg, Timeout}, From, State) when State#state.pending =/= undefined ->
  +    {noreply, queue_request(new_request(Msg, From, Timeout), State)};
  +handle_call({req, Msg, Timeout, Ctx}, From, State) when State#state.pending =/= undefined ->
  +    {noreply, queue_request(new_request(Msg, From, Timeout, Ctx), State)};
  +handle_call({req, Msg, Timeout}, From, State) ->
  +    {noreply, send_request(new_request(Msg, From, Timeout), State)};
  +handle_call({req, Msg, Timeout, Ctx}, From, State) ->
  +    {noreply, send_request(new_request(Msg, From, Timeout, Ctx), State)}.
   
   %% @private
   handle_info({tcp_closed, _Socket}, State) ->
       {stop, normal, State};
  -handle_info({tcp, _Socket, Data}, State=#state{sock=Sock, req = Req, ctx = Ctx, from = From}) ->
  +handle_info({tcp, _Socket, Data}, State=#state{sock = Sock, pending = Pending}) ->
       [MsgCode|MsgData] = Data,
       Resp = riakc_pb:decode(MsgCode, MsgData),
       case Resp of
           #rpberrorresp{} ->
  -            on_error(Req, Ctx, Resp, From),
  -            NewState = dequeue_request(State#state{req = undefined, from = undefined});
  -
  +            NewState1 = maybe_reply(on_error(Pending, Resp, State)),
  +            NewState = dequeue_request(NewState1#state{pending = undefined});
           _ ->
  -            case process_response(Req, Ctx, Resp, State) of
  +            case process_response(Pending, Resp, State) of
                   {reply, Response, NewState0} ->
  -                    %% Send reply and get ready for the next request - send the next one if it's queued up
  -                    gen_server:reply(From, Response),
  -                    NewState = dequeue_request(NewState0#state{req = undefined, from = undefined});
  +                    %% Send reply and get ready for the next request - send the next request
  +                    %% if one is queued up
  +                    NewState1 = send_caller(Response, NewState0),
  +                    NewState = dequeue_request(NewState1#state{pending = undefined});
   
                   {noreply, NewState0} ->
  -                    %% Request has completed with no reply needed, send the next next one if queued up
  -                    NewState = dequeue_request(NewState0#state{req = undefined, from = undefined});
  +                    %% Request has completed with no reply needed, send the next request 
  +                    %% if one is queued up
  +                    NewState = dequeue_request(NewState0#state{pending = undefined});
   
                   {pending, NewState} ->
                       ok %% Request is still pending - do not queue up a new one
  
positive.code:     case queue:out(State#state.queue) of
          {empty, _} ->
              State;
          {{value, {Req, Ctx, From}}, Q2} ->
              send_request(Req, Ctx, From, State#state{queue = Q2})
      end.
  
  %% @private
  
negative.code: %% ====================================================================
  %% internal functions
  %% ====================================================================
  
  maybe_reply({reply, Reply, State}) ->
      send_caller(Reply, State);
  maybe_reply({noreply, State}) ->
      State.
  
  %% @private
  %% Reply to caller - form clause first in case a ReqId/Client was passed
  %% in as the context and gen_server:reply hasn't been called yet.
  send_caller(Msg, State=#state{pending = #request{ctx = {ReqId, Client}, 
                                                   from = undefined}}) ->
      Client ! {ReqId, Msg},
      State;
  send_caller(Msg, State=#state{pending=#request{from = From}=Pending}) when From /= undefined ->
      gen_server:reply(From, Msg),
      State#state{pending = Pending#request{from = undefined}}.
  
  default_timeout(_Op) ->
      60000.
  