anchor.diff:  %% @doc Start the websocket client
   %%
   %% This function will open a connection with the specified remote endpoint building an URL like `ws://Host:PortResource'.
  -%% If the atom `anon' is given as the FsmName, the client will not be registered.
  +%% If the atom `anon' is given as the ClientName, the client will not be registered.
   -spec start(
     Host     :: string(),
     Port     :: inet:port_number(),
     Resource :: string(),
  -  FsmName  :: client_name()
  +  ClientName  :: client_name()
     ) -> pid().
   start(Host, Port, Path, anon) ->
     gen_fsm:start_link(?MODULE, {Host, Port, Path}, [{timeout, 5000}]);
  -start(Host, Port, Path, FsmName) ->
  -  gen_fsm:start_link(FsmName, ?MODULE, {Host, Port, Path}, [{timeout, 5000}]).
  -
  -%% @doc This function will stop the websocket client
  -%%
  -%% Calling this function doesn't mean that the connection will be inmediatelly closed. A
  -%%closing handshake is tryed and only after that the connection is closed.
  +start(Host, Port, Path, ClientName) ->
  +  gen_fsm:start_link(ClientName, ?MODULE, {Host, Port, Path}, [{timeout, 5000}]).
  +
  +%% @doc Stop the websocket client
  +%%
  +%% This is the same as calling {@link stop/1} with the atom `wsecli' as a parameter.
   -spec stop() -> ok.
   stop() ->
     stop(?DEFAULT_REG_NAME).
   
  -%% @doc This function will stop the websocket client
  +%% @doc Stop the websocket client
   %%
   %% Calling this function doesn't mean that the connection will be inmediatelly closed. A
  -%% closing handshake is tryed and only after that the connection is closed. Parameters
  -%% <ul>
  -%% <li>Socket, a pid or atom. Handle to socket that is to be closed.</li>
  -%% </ul>
  --spec stop(Socket::pid()|atom()) -> ok.
  -stop(Socket) ->
  -  gen_fsm:sync_send_all_state_event(Socket, stop).
  +%% closing handshake is tryed and only after that the connection is closed and the client stopped.
  +-spec stop(
  +  Client :: client_name()
  +  ) -> ok.
  +stop(Client) ->
  +  gen_fsm:sync_send_all_state_event(Client, stop).
   
   %% @doc Send data to a remote endpoint
   %%
   %% This is the same as calling {@link send/2} with the atom `wsecli' (see {@link start/3}) as first parameter
  -%%
   -spec send(
       Data::string()
     ) -> ok;
  
positive.code: %%========================================
  %% Types
  %%========================================
  -type encoding() :: text | binary.
  -type client_name() :: {local, atom()} | {global, atom()} | anon.
  
  %%========================================
  %% Constants
  
negative.code: %% @doc Send data to a remote endpoint
  %%
  %% This is the same as calling {@link send/3} whit the third parameter being the type of the Data sent.
  -spec send(
    Client :: client_name(),
    Data :: binary()|string()
    ) -> ok.
  send(Client, Data) when is_binary(Data) ->
    send(Client, Data, binary);
  send(Client, Data) when is_list(Data) ->
    send(Client, Data, text).
  
  %% @doc Send data to a remote endpoint
  %%
  %% Sends data using the specified client, encoding the data as the give type.
  -spec send(
    Client :: client_name(),
    Data :: binary() | string(),
    Type :: encoding()
    ) -> ok.
  send(Client, Data, Type) ->
    gen_fsm:send_event(Client, {send, Data, Type}).
  
  %% @doc Add a callback to be called when a connection is opened
  %%
  %% This is the same as calling {@link on_open/2} with the atom `wsecli' as first parameter.
  -spec on_open(
    Callback :: on_open_callback()
    ) -> any().
  on_open(Callback) ->
    on_open(?DEFAULT_REG_NAME, Callback).
  
  %% @doc Add a callback to be called when a connection is opened
  %%
  %% This callback will be called when the connection is opened (after a successful websocket handshake)
  %%or inmediatelly if added while in the open state. Adding it in any other state will
  %%raise an error.
  -spec on_open(
    Client   :: client_name(),
    Callback :: on_open_callback()
    ) -> any().
  on_open(Client, Callback) ->
    gen_fsm:send_event(Client, {on_open, Callback}).
  
  %% @doc Add a callback to be called when an error occurs
  %%
  %% This is the same as calling {@link on_error/2} with the atom `wsecli' as the first parameter.
  -spec on_error(
    Callback :: on_error_callback()
    ) -> any().
  on_error(Callback) ->
    on_error(?DEFAULT_REG_NAME, Callback).
  
  %% @doc Add a callback to be called when an error occurs
  -spec on_error(
    Client   :: client_name(),
    Callback :: on_error_callback()
    ) -> any().
  on_error(Client, Callback) ->
    gen_fsm:send_all_state_event(Client, {on_error, Callback}).
  
  %% @doc Add a callback to be called when a message arrives
  %%
  %% This is the same as calling {@link on_message/2} with the atom `wsecli' as the first parameter.
  -spec on_message(
    Callback :: on_message_callback()
    ) -> any().
  on_message(Callback) ->
    on_message(?DEFAULT_REG_NAME, Callback).
  
  %% @doc Add a callback to be called when a message arrives
  -spec on_message(
    Client   :: client_name(),
    Callback :: on_message_callback()
    ) -> any().
  on_message(Client, Callback) ->
    gen_fsm:send_all_state_event(Client, {on_message, Callback}).
  
  %% @doc Add a callback to be called when then connection was closed
  %%
  %% This is the same as calling {@link on_close/2} with the atom `wsecli' as the first parameter.
  -spec on_close(
    Callback :: on_close_callback()
    ) -> any().
  on_close(Callback) ->
    on_close(?DEFAULT_REG_NAME, Callback).
  
  %% @doc Add a callback to be called when then connection was closed
  %%
  %% This callback will be called when the connection is successfully closed, i.e. after performing the
  %%websocket closing handshake.
  -spec on_close(
    Client   :: client_name(),
    Callback :: on_close_callback()
    )-> any().
  on_close(Client, Callback) ->
    gen_fsm:send_all_state_event(Client, {on_close, Callback}).
  
  %%========================================
  %% Gen FSM functions
  %%========================================
  %% @hidden
  -spec init(
    {Host::string(), Port::integer(), Resource::string()}
    ) -> {ok, connecting, #data{}}.
  init({Host, Port, Resource}) ->
    {ok, Socket} = gen_tcp:connect(Host, Port, [binary, {reuseaddr, true}, {packet, raw}] ),
  
  