anchor.diff:              exit(attachment_request_failed);
           ResponseCode == 500 ->
               % an error... log and retry
  -            ?LOG_INFO("retrying streaming attachment request due to 500 error: ~s", [Url]),
  +            ?LOG_INFO("retrying couch_rep attachment request in ~p " ++ 
  +                "seconds due to 500 response: ~s", [Pause/1000, Url]),
               Pid ! {self(), fail},
  +            catch ibrowse:stop_worker_process(Conn),
  +            timer:sleep(Pause),
               make_attachment_stub_receiver(Url, Headers, Name, Type, Length, 
  -                Retries - 1)
  +                Retries - 1, 2*Pause)
           end
       end.
   
  
positive.code:     {Name, {Type, {RcvFun, Length}}}.
  
  make_attachment_stub_receiver(Url, Headers, Name, Type, Length) ->
      make_attachment_stub_receiver(Url, Headers, Name, Type, Length, 10).
  
  make_attachment_stub_receiver(Url, _Headers, _Name, _Type, _Length, 0) ->
      ?LOG_ERROR("streaming attachment request failed after 10 retries: ~s", 
          [Url]),
      exit({attachment_request_failed, ?l2b(["failed to replicate ", Url])});
      
  make_attachment_stub_receiver(Url, Headers, Name, Type, Length, Retries) ->
      %% start the process that receives attachment data from ibrowse
      #url{host=Host, port=Port} = ibrowse_lib:parse_url(Url),
      {ok, Conn} = ibrowse:spawn_link_worker_process(Host, Port),
  
negative.code:     Opts = [{stream_to, Pid}, {response_format, binary}],
      ReqId = 
      case ibrowse:send_req_direct(Conn, Url, Headers, get, [], Opts, infinity) of
      {ibrowse_req_id, X} -> 
          X;
      {error, Reason} ->
          ?LOG_INFO("retrying couch_rep attachment request in ~p " ++
              "seconds due to {error, ~p}: ~s", [Pause/1000, Reason, Url]),
          catch ibrowse:stop_worker_process(Conn),
          timer:sleep(Pause),
          make_attachment_stub_receiver(Url, Headers, Name, Type, Length,
              Retries-1, 2*Pause)
      end,
      
      %% tell our receiver about the ReqId it needs to look for
      Pid ! {self(), {set_req_id, ReqId}},
      receive 
      {Pid, {ok, ReqId}} -> 
          ok;
      {'EXIT', Pid, _Reason} ->
          catch ibrowse:stop_worker_process(Conn),
          timer:sleep(Pause),
          make_attachment_stub_receiver(Url, Headers, Name, Type, Length,
              Retries-1, 2*Pause)
      end,
      
      %% wait for headers to ensure that we have a 200 status code
      %% this is where we follow redirects etc
      Pid ! {self(), gimme_status}, 
      receive
      {'EXIT', Pid, attachment_request_failed} ->
          catch ibrowse:stop_worker_process(Conn),
          make_attachment_stub_receiver(Url, Headers, Name, Type, Length,
              Retries-1, Pause);
      {Pid, {status, StreamStatus, StreamHeaders}} -> 
          ?LOG_DEBUG("streaming attachment Status ~p Headers ~p",
              [StreamStatus, StreamHeaders]),
  