anchor.diff:    #conn_state{database = Database, read_mode = RMode, write_mode = WMode}.
   
   
  -
   %% @private
   %% Register this process if needed
   try_register(Options) ->
  
positive.code: disconnect(Worker) ->
    gen_server:cast(Worker, halt).
  
  
  init(Options) ->
    Proxy = proplists:get_value( proxy, Options, false ),
    init_local( Proxy, Options ).
  
  init_local( false, Options ) ->
    {ok, Socket} = mc_auth:connect_to_database(Options),
    ConnState = form_state(Options),
    try_register(Options),
    proc_lib:init_ack({ok, self()}),
    mc_auth:auth(Socket, Options, ConnState#conn_state.database),
    gen_server:enter_loop(?MODULE, [], #state{socket = Socket, conn_state = ConnState});
  
  init_local( true, Options ) ->
    {ok, Socket} = mc_auth:connect_to_database(Options),
    ConnState = form_state(Options),
    try_register(Options),
    mc_auth:auth(Socket, Options, ConnState#conn_state.database),
    {ok, #state{socket = Socket, conn_state = ConnState} }.
  
  
  
  update_dbcoll( { Db, _ }, Coll ) ->
    { Db, Coll };
  update_dbcoll( _, Coll ) ->
    Coll.
  
  
  collection( #'query'{ collection = Coll } ) ->
    Coll;
  collection( #'insert'{ collection = Coll } ) ->
    Coll;
  collection( #'update'{ collection = Coll } ) ->
    Coll;
  collection( #'delete'{ collection = Coll } ) ->
    Coll.
  
  handle_call(NewState = #conn_state{}, _, State = #state{conn_state = OldState}) ->  % update state, return old
    {reply, {ok, OldState}, State#state{conn_state = NewState}};
  
  
  handle_call(#ensure_index{collection = Coll, index_spec = IndexSpec}, _, State = #state{conn_state = ConnState, socket = Socket}) % ensure index request with insert request
    ->
  
    Key = maps:get(<<"key">>, IndexSpec),
    Defaults = {<<"name">>, mc_worker_logic:gen_index_name(Key), <<"unique">>, false, <<"dropDups">>, false},
    Index = bson:update(<<"ns">>, mongo_protocol:dbcoll(ConnState#conn_state.database, Coll), bson:merge(IndexSpec, Defaults)),
    {ok, _} = mc_worker_logic:make_request(
        Socket,
        ConnState#conn_state.database,
        #insert{ collection = update_dbcoll( Coll, <<"system.indexes">> ),
        documents = [Index]}),
    {reply, ok, State};
  
  
  
  handle_call(Request, From, State = #state{socket = Socket, conn_state = ConnState = #conn_state{}, request_storage = ReqStor})
    when is_record(Request, insert); is_record(Request, update); is_record(Request, delete) % write requests
    ->
  
    case ConnState#conn_state.write_mode of
      unsafe ->   %unsafe (just write)
        {ok, _} = mc_worker_logic:make_request(Socket, ConnState#conn_state.database, Request),
        {reply, ok, State};
  
      SafeMode -> %safe (write and check)
        Params = case SafeMode of safe -> {}; {safe, Param} -> Param end,
        ConfirmWrite = #'query'{ % check-write read request
          batchsize = -1,
          collection = update_dbcoll( collection( Request ), <<"$cmd">> ),
          selector = bson:append({<<"getlasterror">>, 1}, Params)
        },
        {ok, Id} = mc_worker_logic:make_request(Socket, ConnState#conn_state.database, [Request, ConfirmWrite]), % ordinary write request
        RespFun = mc_worker_logic:get_resp_fun(Request, From),
        UReqStor = dict:store(Id, RespFun, ReqStor),  % save function, which will be called on response
        {noreply, State#state{request_storage = UReqStor}}
    end;
  
  
  
  handle_call(Request, From, State = #state{socket = Socket, request_storage = RequestStorage, conn_state = CS}) % read requests
    when is_record(Request, 'query'); is_record(Request, getmore)
    ->
  
    UpdReq = case is_record(Request, 'query') of
               true ->
                  case Request#'query'.sok_overriden of
                    true ->
                      Request;
                    false ->
                      Request#'query'{slaveok = CS#conn_state.read_mode =:= slave_ok}
                  end;
               false -> Request
             end,
  
    {ok, Id} = mc_worker_logic:make_request(Socket, CS#conn_state.database, UpdReq),
  
    RespFun = mc_worker_logic:get_resp_fun(UpdReq, From),  % save function, which will be called on response
    URStorage = dict:store(Id, RespFun, RequestStorage),
    {noreply, State#state{request_storage = URStorage}};
  
  
  handle_call(Request, _, State = #state{socket = Socket, conn_state = ConnState})
    when is_record(Request, killcursor)
    ->
  
    {ok, _} = mc_worker_logic:make_request(Socket, ConnState#conn_state.database, Request),
    {reply, ok, State};
  
  handle_call({stop, _}, _From, State) -> % stop request
    {stop, normal, ok, State}.
  
  
negative.code:   {Responses, Pending} = mc_worker_logic:decode_responses(Buffer),
    UReqStor = mc_worker_logic:process_responses(Responses, RequestStorage),
    {noreply, State#state{buffer = Pending, request_storage = UReqStor}};
  handle_info({tcp_closed, _Socket}, State) ->
    {stop, tcp_closed, State};
  handle_info({tcp_error, _Socket, Reason}, State) ->
    {stop, Reason, State}.
  
  
  %% @hidden
  terminate(_, State) ->
      catch gen_tcp:close(State#state.socket).
  
  %% @hidden
  code_change(_Old, State, _Extra) ->
    {ok, State}.
  
  
  %% @private
  