anchor.diff:        format_header(Name, Value)
     end || {Name, Value} <- ReqHeaders],
   
  -  case gen_tcp:send(Conn, [FormattedHeaders, <<"\r\n">>]) of
  -    ok ->
  -      next(Req, State, fun req_body/2);
  -    {error, _Error} ->
  +  send(Req, State, [FormattedHeaders, <<"\r\n">>], fun req_body/2).
  +
  +req_body(Req, State) ->
  +  case cowboy_req:has_body(Req) of
  +    true ->
  +      case exported(Req, State, req_body, 3) of
  +        true ->
  +          %% TODO should we catch the errors?
  +          %% TODO expose max body size variable
  +          {ok, Body, Req2} = cowboy_req:body(Req),
  +          {Body2, Req3, State2} = call(Req2, State, req_body, Body),
  +          next(Req3, State2#state{req_body = Body2}, fun send_req_body/2);
  +        _ ->
  +          next(Req, State, fun chunk_req_body/2)
  +      end;
  +    _ ->
  +      next(Req, State, fun get_res_status/2)
  +  end.
  +
  +send_req_body(Req, State = #state{req_body = Body}) ->
  +  %% TODO can we send all of the body at once and have it buffer automatically or do we need to chunk it?
  +  send(Req, State, Body, fun get_res_status/2).
  +
  +chunk_req_body(Req, State) ->
  +  %% TODO expose max body size variable
  +  case cowboy_req:stream_body(Req) of
  +    {ok, Body, Req2} ->
  +      send(Req2, State, Body, fun chunk_req_body/2);
  +    {done, Req2} ->
  +      next(Req2, State, fun get_res_status/2);
  +    {error, _Reason} ->
  +      %% TODO send back a better error message
  +      next(Req, State, 400)
  +  end.
  +
  +get_res_status(Req, State = #state{conn = Conn}) ->
  +  inet:setopts(Conn, [{active, once}]),
  +  receive
  +    {http, Conn, {http_response, _HttpVersion, _HttpRespCode, ResStatus}} ->
  +      next(Req, State#state{res_status = ResStatus}, fun get_res_headers/2);
  +    {tcp_closed, Conn} ->
         next(Req, State, 502)
  -  end.
  -
  -req_body(Req, State) ->
  -  respond(Req, State, 200).
  +  %% TODO add a response timeout
  +  % after Timeout
  +  end.
  +
  +get_res_headers(Req, State = #state{conn = Conn, res_status = ResStatus, res_headers = ResHeaders}) ->
  +  %% TODO how do we make it not forward the headers onto the client?
  +  inet:setopts(Conn, [{active, once}]),
  +  receive
  +    {http, Conn, {http_header, _, 'Connection', _, _Val}} ->
  +      get_res_headers(Req, State);
  +    {http, Conn, {http_header, _, Field, _, Value}} ->
  +      NewResHeaders = [{atom_to_binary(Field), Value}|ResHeaders],
  +      get_res_headers(Req, State#state{res_headers = NewResHeaders});
  +    {http, Conn, http_eoh} ->
  +      {ManipulatedHeaders, Req2, State2} = case call(Req, State, res_headers, ResHeaders) of
  +        no_call ->
  +          {ResHeaders, Req, State};
  +        Result ->
  +          Result
  +      end,
  +      {ok, Req3} = cowboy_req:chunked_reply(ResStatus, ManipulatedHeaders, Req2),
  +      next(Req3, State2, fun get_res_body/2)
  +  %% TODO add a response timeout
  +  % after Timeout
  +  end.
  +
  +get_res_body(Req, State = #state{conn = Conn}) ->
  +  inet:setopts(Conn, [{active, once}, {packet, raw}]),
  +  receive
  +    {tcp, Conn, Data} ->
  +      ok = cowboy_req:chunk(Data, Req),
  +      get_res_body(Req, State);
  +    {tcp_closed, Conn} ->
  +      terminate(Req, State);
  +    Other ->
  +      io:format("~p~n", [Other]),
  +      get_res_body(Req, State)
  +  %% TODO add a response timeout
  +  % after Timeout
  +  end.
  +
  +
   
   % internal
   
  
positive.code:     {<<Prefix/binary, "-port">>, integer_to_binary(Port)},
      {<<Prefix/binary, "-path">>, Path}
    ].
  
  %% Protocol
  
  
negative.code:       next(Req, State, 502)
    end.
  
  init_request(Req, State=#state{method=Method}) ->
    {Path, Req2} = cowboy_req:path_info(Req),
    {Version, Req3} = cowboy_req:version(Req2),
    Message = format_request(Method, Path, Version),
    send(Req3, State, Message, fun forwarded_header_prefix/2).
  
  forwarded_header_prefix(Req, State=#state{backend=Backend, req_headers=ReqHeaders}) ->
    case call(Req, State, forwarded_header_prefix) of
  