anchor.diff:      ->
     {ok, 0};
   insert_terminal_raw_packet(RawID, RawPacket, Type, Timeout) ->
  -  execute(1000, insert, {data, packets, [{id, now2id()}, {raw_id, RawID}, {data, RawPacket}, {type, Type}]}, Timeout).
  -
  +  PacketId = now2id(),
  +  case execute(1000, function, {data, packet, [PacketId, RawPacket, RawID, Type]}, Timeout) of
  +    [[{packet, PacketId}]] -> {ok, PacketId};
  +    _ -> {ok, 0}
  +  end.
   
   insert_terminal_packet(PacketID, _Module, Type, _Table, _Packet, _Timeout)
       when PacketID =:= 0; PacketID =:= undefined; PacketID =:= null;
  
positive.code:   trace("terminal answer"),
    case hooks:get(raw_id) of
      undefined ->
        trace("now raw id"),
        stop;
      RawDataID ->
        {ok, _RawDataID} = execute(1000, insert, {data, raws, [{id, RawDataID}, {answer, Answer}, {answered, Module}]}, Timeout),
        ok
    end.
  
  
negative.code:   trace("new connection accepted"),
    {ok, {RemoteIP, RemotePort}} = inet:peername(Socket),
    {ok, {LocalIP, LocalPort}} = inet:sockname(Socket),
    ConnectionId = now2id(),
    [[{open, ConnectionID}]] = execute(1000, function, {connection, open,
                                 [ConnectionId, Proto, LocalIP, LocalPort, RemoteIP, RemotePort]},
                Timeout),
    hooks:set(Pid, connection_id, ConnectionID),
    debug("~w connection id is ~w", [Pid, ConnectionID]),
    ok.
  
  connection_closed(Pid, normal, Timeout) ->
    case hooks:get(Pid, connection_id) of
      undefined ->
        alert("connection closed, but no connection ID specified for ~w", [Pid]),
        ok;
      ConnectionId ->
        debug("closing connection ~w", [ConnectionId]),
        [[{close, ConnectionId}]] = execute(-10, function, {connection, close, [ConnectionId]}, Timeout),
        trace("connection closed"),
        ok
    end;
  connection_closed(Pid, {broken, Data}, Timeout) ->
    ConnectionId = hooks:get(Pid, connection_id),
    Id = now2id(),
    execute(-10, function, {data, broken, [Id, Data, ConnectionId]}, Timeout),
    connection_closed(Pid, normal, Timeout);
  connection_closed(Pid, {incomplete, Data}, Timeout) ->
    connection_closed(Pid, {broken, Data}, Timeout);
  connection_closed(Pid, {function_clause, [{_Module, parse, [Data], _FileInfo} | _ ]}, Timeout) ->
    connection_closed(Pid, {broken, Data}, Timeout);
  connection_closed(Pid, Reason, Timeout) ->
    warning("closed connection with unknown reason ~w", [Reason]),
    connection_closed(Pid, normal, Timeout).
  
  terminal_uin(_Pid, _, undefined, _Timeout) ->
    ok;
  terminal_uin(_Pid, Module, UIN, Timeout) ->
    TerminalId = get_terminal_id({Module, UIN}, Timeout),
    ConnectionId = hooks:get(connection_id),
    debug("setting terminal ~w on connection ~w", [TerminalId, ConnectionId]),
    hooks:set(terminal_id, TerminalId),
    execute(1000, function, {connection, set_terminal, [ConnectionId, TerminalId]}, Timeout),
    ok.
  
  terminal_raw_data(_Pid, _Module, _UIN, RawData, Timeout) ->
    trace("terminal raw data"),
    ConnectionID = hooks:get(connection_id),
    RawID = now2id(),
    case execute(1000, function, {data, raw, [RawID, RawData, ConnectionID]}, Timeout) of
      [[{raw, null}]] ->
        debug("data repeat"),
        hooks:delete(raw_id),
        stop;
      [[{raw, RawID}]] ->
        debug("raw id is ~w", [RawID]),
        hooks:set(raw_id, RawID),
        ok
  