anchor.diff:    application:start(mongodb),
     application:start(egeoip),
   
  -  ?DEBUG("mod_copilot_server:init ~p", [Args]),
  -  %Host = {lists:keyfind(mongodb_host, 1, Args), lists:keyfind(mongodb_port, 1, Data)},
  -  %{ok, Conn} = mongo:connect(Host),
  -
  -  State = [{ok, ok}, {mod_opts, Args}],
  +  Host = case proplists:get_value(mongodb, Args) of
  +    undefined -> {localhost, 27017};
  +    Value -> Value
  +  end,
  +  {ok, Conn} = mongo:connect(Host),
  +
  +  State = [{opts, Args},   % Module's settings, as defined in the ejabberd.cfg
  +           {mongo, Conn}   % MongoDB connection
  +          ],
     {ok, State}.
   
   handle_call(_Request, _From, State) ->
     Reply = ok,
     {reply, Reply, State}.
   
  -handle_cast({userconnect, JID, IPAddress}, State) ->
  -  GeoData = lookup_ip(IPAddress),
  -  {noreply, State}.
  -
  +%@doc Handles cases when agents connect and disconnect from the server
  +handle_cast({connect, JID, IPAddress}, State) ->
  +  Doc = doc_create_connection(JID, IPAddress),
  +  mongo_do(State, copilot, fun () -> mongo:save(connections, Doc) end);
  +handle_cast({disconnect, {User, Host, Resource} = JID, IPAddress}, State) ->
  +  mongo_do(State, copilot, fun () ->
  +    JIDString = User ++ "@" ++ Host ++ "/" ++ Resource,
  +    Cursor = mongo:find(connections, {jid, JIDString,
  +                                      active, true}),
  +    NewDoc = doc_close_connection(mongo:next(Cursor), JID, IPAddress),
  +    mongo:close_cursor(Cursor),
  +    mongo:save(connections, NewDoc)
  +  end);
   handle_cast(_Msg, State) ->
     {noreply, State}.
   
  
positive.code:    {"event", Event},
     {Type, Value}].
  
  % Sends a reportEvent command to the Monitor
  report_event(Server, Event) ->
    To = gen_mod:get_module_opt(Server, ?MODULE, monitor_jid, "mon@localhost"),
    XmlBody = {xmlelement, "message", [{"from", ?JID},
  
negative.code: -include("jlib.hrl").
  
  % Required by gen_mod
  -export([start/2, stop/1, on_set_presence/4, on_unset_presence/4]).
  % Required by gen_server
  -export([start_link/2]).
  -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  