anchor.diff:      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]};
  -
  +    session_creation_body(Version, Lang, Rid, To, Sid);
   wrap_elem(["</", <<"stream:stream">>, ">"], #state{sid=Sid, rid=Rid}) ->
  +    session_termination_body(Rid, Sid);
  +wrap_elem(Element, #state{sid = Sid, rid=Rid}) ->
  +    (empty_body(Rid, Sid))#xmlel{children = [Element]}.
  +
  +session_creation_body(Rid, To) ->
  +    session_creation_body(<<"1.0">>, <<"en">>, Rid, To, nil).
  +
  +session_creation_body(Version, Lang, Rid, To, Sid) ->
  +    empty_body(Rid, Sid,
  +               [{<<"content">>, <<"text/xml; charset=utf-8">>},
  +                {<<"xmlns:xmpp">>, ?NS_BOSH},
  +                {<<"xmpp:version">>, Version},
  +                {<<"hold">>, <<"1">>},
  +                {<<"wait">>, <<"60">>},
  +                {<<"xml:lang">>, Lang},
  +                {<<"to">>, To}]
  +               ++ [{<<"xmpp:restart">>, <<"true">>} || Sid =/= nil]).
  +
  +session_termination_body(Rid, Sid) ->
  +    Body = empty_body(Rid, Sid, [{<<"type">>, <<"terminate">>}]),
  +    Body#xmlel{children = [escalus_stanza:presence(<<"unavailable">>)]}.
  +
  +empty_body(Rid, Sid) ->
  +    empty_body(Rid, Sid, []).
  +
  +empty_body(Rid, Sid, ExtraAttrs) ->
       #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)}.
  +                attrs = common_attrs(Rid, Sid) ++ ExtraAttrs}.
   
   common_attrs(Rid) ->
       [{<<"rid">>, pack_rid(Rid)},
  -     {<<"xmlns">>, <<"http://jabber.org/protocol/httpbind">>}].
  +     {<<"xmlns">>, ?NS_HTTP_BIND}].
  +
   common_attrs(Rid, nil) ->
       common_attrs(Rid);
   common_attrs(Rid, Sid) ->
  
positive.code:     {ok, Parser} = exml_stream:new_parser(),
      {ok, #state{owner = Owner,
                  url = {HostStr, Port, binary_to_list(Path)},
                  parser = Parser, rid=InitRid}}.
  
  handle_call(get_transport, _From, State) ->
      {reply, transport(State), State};
  
negative.code: 
  -include_lib("exml/include/exml_stream.hrl").
  -include("include/escalus.hrl").
  -include("include/escalus_xmlns.hrl").
  
  %% API exports
  -export([connect/1,
  