anchor.diff:           handle_info/2,
            terminate/2,
            code_change/3]).
  +
  +%% BOSH XML elements
  +-export([session_creation_body/2, session_creation_body/5,
  +         session_termination_body/2,
  +         empty_body/2, empty_body/3]).
   
   -define(WAIT_FOR_SOCKET_CLOSE_TIMEOUT, 200).
   -define(SERVER, ?MODULE).
  
positive.code:     Version = proplists:get_value(<<"version">>, Attrs, <<"1.0">>),
      Lang = proplists:get_value(<<"xml:lang">>, Attrs, <<"en">>),
      To = proplists:get_value(<<"to">>, Attrs, <<"localhost">>),
      #xmlel{name = <<"body">>, attrs=common_attrs(Rid, Sid) ++ [
              {<<"content">>, <<"text/xml; charset=utf-8">>},
              {<<"xmlns:xmpp">>, <<"urn:xmpp:xbosh">>},
              {<<"xmpp:version">>, Version},
              {<<"hold">>, <<"1">>},
              {<<"wait">>, <<"60">>},
              {<<"xml:lang">>, Lang},
              {<<"to">>, To}
              ] ++ [{<<"xmpp:restart">>, <<"true">>} || Sid =/= nil]};
  
  wrap_elem(["</", <<"stream:stream">>, ">"], #state{sid=Sid, rid=Rid}) ->
      #xmlel{name = <<"body">>,
                  attrs = common_attrs(Rid, Sid) ++ [{<<"type">>, <<"terminate">>}],
                  children = [#xmlel{name = <<"presence">>,
                                     attrs = [{<<"type">>, <<"unavailable">>},
                                              {<<"xmlns">>, <<"jabber:client">>}]}]};
  
  wrap_elem(Element, #state{sid = Sid, rid=Rid}) ->
      #xmlel{name = <<"body">>,
                  attrs = common_attrs(Rid, Sid),
                  children = [Element]}.
  
  empty_body(#state{sid = Sid, rid=Rid}) ->
      #xmlel{name = <<"body">>,
                  attrs = common_attrs(Rid, Sid)}.
  
  common_attrs(Rid) ->
      [{<<"rid">>, pack_rid(Rid)},
       {<<"xmlns">>, <<"http://jabber.org/protocol/httpbind">>}].
  common_attrs(Rid, nil) ->
      common_attrs(Rid);
  common_attrs(Rid, Sid) ->
  
negative.code:     NS = handle_data(Body, S#state{requests = S#state.requests - 1}),
      NNS = case NS#state.requests == 0 of
          true ->
              send(Transport, empty_body(NS#state.rid, NS#state.sid), NS);
          false ->
              NS
      end,
  