anchor.diff:             [{<<"id">>, MsgId}]},
       {ok, AckBin} = toml:term_2_binary(Ack),
       AckMessage = #message{id = MsgId, bin = AckBin},
  -    ok = gen_tcp:send(Device#device.socket, AckBin),
  -    NewMsgCache = [AckMessage|State#state.msg_cache],
  +    ok = gen_tcp:send(Socket, AckBin),
  +    NewDeviceMsgCache = [AckMessage|Device#device.msg_cache],
  +    NewDevice = Device#device{msg_cache = NewDeviceMsgCache},
  +    NewDeviceList = lists:keystore(Socket,
  +                                   #device.socket,
  +                                   State#state.device_list,
  +                                   NewDevice),
  +    NewStateTemp = State#state{device_list = NewDeviceList},
   
       NewAttrs = add_ts_from(Attrs, State#state.user_id),
       NewToml = {Type, NewAttrs},
       {ok, NewTomlBin} = toml:term_2_binary(NewToml),
       Message = #message{id = MsgId, bin = NewTomlBin},
  -    ok = send_msg_2_multiple_device(OtherDeivces, Message),
  -    {ok, Message, State#state{msg_cache = NewMsgCache}}.
  +    {ok, NewState} = send_msg_2_multiple_device(OtherDeivces,
  +                                                Message,
  +                                                NewStateTemp,
  +                                                ignore),
  +    {ok, Message, NewState}.
   
   
   add_ts_from(Attrs, UserId) ->
  
positive.code: 
  handle_info({replace_socket, Message, #device{socket = Socket} = Device},
              #state{device_list = DeviceList} = State) ->
      NewState = case lists:keytake(Device#device.name,
                                    #device.name, DeviceList) of
          {value, #device{socket = OldSocket} = OldDevice, OtherDeivces} ->
  
negative.code: %% Internal functions
  %% ===================================================================
  
  send_msg_cache([Device|T]) ->
      ok = loop_send_msg(Device#device.socket, Device#device.msg_cache),
      send_msg_cache(T);
  send_msg_cache([]) ->
      ok.
  
  
  loop_send_msg(Socket, [Message|T]) ->
      ok = gen_tcp:send(Socket, Message#message.bin),
      loop_send_msg(Socket, T);
  loop_send_msg(_, []) ->
      ok.
  
  
  merge_msg_cache(State, OldMsgCache) ->
      {ok, NewDeviceList} = merge_msg_cache(State#state.device_list,
                                            OldMsgCache,
                                            []),
      NewMsgCache = State#state.msg_cache ++ OldMsgCache,
      NewState = State#state{device_list = NewDeviceList,
                             msg_cache = NewMsgCache},
      {ok, NewState}.
  merge_msg_cache([Device|T], OldMsgCache, NewDeviceList) ->
      NewDeviceMsgCache = Device#device.msg_cache ++ OldMsgCache,
      NewDevice = Device#device{msg_cache = NewDeviceMsgCache},
      ok = loop_send_msg(NewDevice#device.socket, NewDevice#device.msg_cache),
      merge_msg_cache(T, OldMsgCache, [NewDevice|NewDeviceList]);
  merge_msg_cache([], _, NewDeviceList) ->
      {ok, NewDeviceList}.
  
  
  send_msg_2_multiple_device([#device{socket = Socket} = Device|T],
                              Message,
                              State,
                              Mode) ->
      ok = gen_tcp:send(Socket, Message#message.bin),
      NewDeviceMsgCache = [Message|Device#device.msg_cache],
      NewDevice = Device#device{msg_cache = NewDeviceMsgCache},
      NewDeviceList = lists:keystore(Socket,
                                     #device.socket,
                                     State#state.device_list,
                                     NewDevice),
      NewState = State#state{device_list = NewDeviceList},
      send_msg_2_multiple_device(T, Message, NewState, Mode);
  send_msg_2_multiple_device([], Message, State, save) ->
      NewMsgCache = [Message|State#state.msg_cache],
      NewState = State#state{msg_cache = NewMsgCache},
      {ok, NewState};
  send_msg_2_multiple_device([], _, State, ignore) ->
      {ok, State}.
  
  
  setopts(Socket) ->
  