anchor.diff:  -compile([export_all, {no_auto_import, [error/2]}]).
   -include_lib("eunit/include/eunit.hrl").
   
  -%%%%%%%%%%%%%
  -%%% The socket.io server-side protocol as processed by https://github.com/3rd-Eden/Socket.IO-node/blob/master/lib/parser.js
  -%%%%%%%%%%%%%
  -
  -%% Framing is only given when no other suitable mode exists. In this case,
  -%% XHR-multipart will already frame these.
  -%% It is not defined whether we should 
  +%% The source code was taken and modified from https://github.com/yrashk/socket.io-erlang/blob/master/src/socketio_data_v1.erl
  +
   -define(FRAME, 16#fffd).
  --define(BINFRAME, <<65533>>).
  --define(RESERVED_EVENTS, [<<"message">>, <<"connect">>, <<"disconnect">>,
  -                          <<"open">>, <<"close">>, <<"error">>, <<"retry">>,
  -                          <<"reconnect">>]).
  -disconnect("") -> <<"0">>;
  -disconnect(Endpoint) -> [<<"0::">>,Endpoint].
  -
  -heartbeat() -> <<"2">>.
  +
  +disconnect(<<>>) ->
  +    <<"0">>;
  +disconnect(Endpoint) ->
  +    <<"0::", Endpoint/binary>>.
  +
  +heartbeat() ->
  +    <<"2">>.
   
   message(Id, EndPoint, Msg) when is_integer(Id) ->
  -    [<<"3:">>, integer_to_list(Id), $:, EndPoint, $:, Msg];
  -message(Id, EndPoint, Msg) when is_list(Id) ->
  -    [<<"3:">>, Id, $:, EndPoint, $:, Msg].
  +    IdBin = binary:list_to_bin(integer_to_list(Id)),
  +    <<"3:", IdBin/binary, ":", EndPoint/binary, ":", Msg/binary>>;
  +message(Id, EndPoint, Msg) when is_binary(Id) ->
  +    <<"3:", Id/binary, ":", EndPoint/binary, ":", Msg/binary>>.
   
   json(Id, EndPoint, Msg) when is_integer(Id) ->
  -    [<<"4:">>, integer_to_list(Id), $:, EndPoint, $:, jsx:term_to_json(Msg)];
  -json(Id, EndPoint, Msg) when is_list(Id) ->
  -    [<<"4:">>, Id, $:, EndPoint, $:, jsx:term_to_json(Msg)].
  -
  -event(Id, EndPoint, Event, Msg) when is_integer(Id) ->
  -    case lists:member(iolist_to_binary(Event), ?RESERVED_EVENTS) of
  -        true -> erlang:error(badarg);
  -        false ->
  -            [<<"5:">>, integer_to_list(Id), $:, EndPoint, $:, Event,
  -                ?BINFRAME, jsx:term_to_json(Msg)]
  -    end;
  -event(Id, EndPoint, Event, Msg) when is_list(Id) ->
  -    case lists:member(iolist_to_binary(Event), ?RESERVED_EVENTS) of
  -        true -> erlang:error(badarg);
  -        false ->
  -            [<<"5:">>, Id, $:, EndPoint, $:, Event,
  -                ?BINFRAME, jsx:term_to_json(Msg)]
  -    end.
  -
  -ack(Id) -> [<<"6:::">>, integer_to_list(Id)].
  -
  -ack(Id, Data) ->
  -    [<<"6:::">>, integer_to_list(Id), $+, jsx:term_to_json(Data)].
  +    IdBin = binary:list_to_bin(integer_to_list(Id)),
  +    JsonBin = jsx:term_to_json(Msg),
  +    <<"4:", IdBin/binary, ":", EndPoint/binary, ":", JsonBin/binary>>;
  +json(Id, EndPoint, Msg) when is_binary(Id) ->
  +    JsonBin = jsx:term_to_json(Msg),
  +    <<"4:", Id/binary, ":", EndPoint/binary, ":", JsonBin/binary>>.
   
   error(EndPoint, Reason) ->
       [<<"7::">>, EndPoint, $:, Reason].
  -
   error(EndPoint, Reason, Advice) ->
       [<<"7::">>, EndPoint, $:, Reason, $+, Advice].
   
  
positive.code:     ?assertEqual(<<"2">>, heartbeat()).
  
  message_test_() ->
      [?_assertEqual(<<"3:1::blabla">>, iolist_to_binary(message(1, "", "blabla"))),
       ?_assertEqual(<<"3:2::bla">>, iolist_to_binary(message(2, "", "bla"))),
       ?_assertEqual(<<"3:::bla">>, iolist_to_binary(message("", "", "bla"))),
       ?_assertEqual(<<"3:4+:b:bla">>, iolist_to_binary(message("4+", "b", "bla"))),
       ?_assertEqual(<<"3::/test:bla">>, iolist_to_binary(message("", "/test", "bla")))].
  
  json_test_() ->
      [?_assertEqual(<<"4:1::{\"a\":\"b\"}">>,
                    iolist_to_binary(json(1, "", [{<<"a">>,<<"b">>}]))),
       %% No demo for this, but the specs specify it
       ?_assertEqual(<<"4:1:/test:{\"a\":\"b\"}">>,
                    iolist_to_binary(json(1, "/test", [{<<"a">>,<<"b">>}]))),
       ?_assertEqual(<<"4:1+:/test:{\"a\":\"b\"}">>,
                    iolist_to_binary(json("1+", "/test", [{<<"a">>,<<"b">>}]))),
       ?_assertEqual(<<"4::/test:{\"a\":\"b\"}">>,
           iolist_to_binary(json("", "/test", [{<<"a">>,<<"b">>}])))].
  
  event_test_() ->
      [
          ?_assertEqual(<<"5:1:/test:car", ?FRAME, "{\"a\":\"b\"}">>,
              iolist_to_binary(event(1,"/test","car",[{<<"a">>,<<"b">>}]))),
          ?_assertEqual(<<"5:::car", ?FRAME, "{\"a\":\"b\"}">>,
              iolist_to_binary(event("","",<<"car">>,[{<<"a">>,<<"b">>}]))),
          ?_assertEqual(<<"5:3+::car", ?FRAME, "{\"a\":\"b\"}">>,
              iolist_to_binary(event("3+","",<<"car">>,[{<<"a">>,<<"b">>}]))),
          ?_assertError(badarg, event("","","message",[])),
          ?_assertError(badarg, event("","",<<"message">>,[])),
          ?_assertError(badarg, event("","","connect",[])),
          ?_assertError(badarg, event("","",<<"connect">>,[])),
          ?_assertError(badarg, event("","","disconnect",[])),
          ?_assertError(badarg, event("","",<<"disconnect">>,[])),
          ?_assertError(badarg, event("","","open",[])),
          ?_assertError(badarg, event("","",<<"open">>,[])),
          ?_assertError(badarg, event("","","close",[])),
          ?_assertError(badarg, event("","",<<"close">>,[])),
          ?_assertError(badarg, event("","","error",[])),
          ?_assertError(badarg, event("","",<<"error">>,[])),
          ?_assertError(badarg, event("","","retry",[])),
          ?_assertError(badarg, event("","",<<"retry">>,[])),
          ?_assertError(badarg, event("","","reconnect",[])),
          ?_assertError(badarg, event("","",<<"reconnect">>,[]))
      ].
  
  ack_test_() ->
      [?_assertEqual(<<"6:::4">>, iolist_to_binary(ack(4))),
       ?_assertEqual(<<"6:::4+[\"A\",\"B\"]">>,
           iolist_to_binary(ack(4,[<<"A">>,<<"B">>])))].
  
  error_test_() ->
      %% No example, liberal interpretation
      [?_assertEqual(<<"7::end:you+die">>,
              iolist_to_binary(error("end","you","die"))),
          ?_assertEqual(<<"7:::you">>, iolist_to_binary(error("","you")))].
  
  %% DECODING TESTS
  d_disconnect_test_() ->
  
negative.code:     end.
  
  id(X) -> id(X, "").
  id(<<$:, Rest/binary>>, "") ->
      {<<>>, Rest};
  id(<<$:, Rest/binary>>, Acc) ->
      L = lists:reverse(Acc),
      {list_to_integer(L), Rest};
  