anchor.diff:                  {ok, DbHandle} ->
                       case allocate_statement(Mod, Sock, DbHandle) of
                           {ok, StmtHandle} ->
  -                            {ok, State#state{sock = Sock, db_handle = DbHandle,
  -                                stmt_handle = StmtHandle}};
  +                            {ok, State#state{db_handle = DbHandle, stmt_handle = StmtHandle}};
                           {error, _Reason} ->
  -                            {{error, "Can't allocate statement"},
  -                                State#state{sock = Sock, db_handle = DbHandle}}
  +                            {{error, "Can't allocate statement"}, State#state{db_handle = DbHandle}}
                       end;
                   {error, _Reason} ->
  -                    {R, State#state{sock = Sock}}
  +                    {R, State}
               end;
  -        op_reject -> {{error, "Connection Rejected"}, State#state{sock = Sock}}
  -    end.
  -
  -attach_database(Mod, Sock, User, Password, Database) ->
  -    Mod:send(Sock,
  -        efirebirdsql_op:op_attach(User, Password, Database)),
  -    case efirebirdsql_op:get_response(Mod, Sock) of
  -        {op_response,  {ok, Handle, _}} -> {ok, Handle};
  -        _ -> {error, "Can't attach Database"}
  -    end.
  -
  -create_database(Mod, Sock, User, Password, Database, PageSize) ->
  -    Mod:send(Sock,
  -        efirebirdsql_op:op_create(User, Password, Database, PageSize)),
  -    case efirebirdsql_op:get_response(Mod, Sock) of
  -        {op_response,  {ok, Handle, _}} -> {ok, Handle};
  -        _ -> {error, "Can't create database"}
  -    end.
  -
  +        op_reject -> {{error, "Connection Rejected"}, State}
  +    end.
  +
  +detach(Mod, Sock, DbHandle) ->
  +    Mod:send(Sock, efirebirdsql_op:op_detach(DbHandle)),
  +    case efirebirdsql_op:get_response(Mod, Sock) of
  +        {op_response,  {ok, _, _}} -> ok;
  +        _ -> {error, "Detach failed"}
  +    end.
  +
  +
  +%% Transaction
   begin_transaction(Mod, Sock, DbHandle, Tpb) ->
       Mod:send(Sock,
           efirebirdsql_op:op_transaction(DbHandle, Tpb)),
  
positive.code:     case gen_tcp:connect(Host, Port, SockOptions) of
          {ok, Sock} ->
              {R, NewState} = connect(gen_tcp, Sock, Host, Username, Password, Database, IsCreateDB, PageSize, State),
              {reply, R, NewState};
          Error = {error, _} ->
              {reply, Error, State}
      end;
  
negative.code:                 stmt_type,
                  xsqlvars = [],
                  rows = []}).
  
  %% Create connection and allocate statement, Close(detatch) database.
  attach_database(Mod, Sock, User, Password, Database) ->
      Mod:send(Sock,
          efirebirdsql_op:op_attach(User, Password, Database)),
      case efirebirdsql_op:get_response(Mod, Sock) of
          {op_response,  {ok, Handle, _}} -> {ok, Handle};
          _ -> {error, "Can't attach Database"}
      end.
  
  create_database(Mod, Sock, User, Password, Database, PageSize) ->
      Mod:send(Sock,
          efirebirdsql_op:op_create(User, Password, Database, PageSize)),
      case efirebirdsql_op:get_response(Mod, Sock) of
          {op_response,  {ok, Handle, _}} -> {ok, Handle};
          _ -> {error, "Can't create database"}
      end.
  
  allocate_statement(Mod, Sock, DbHandle) ->
      Mod:send(Sock,
          efirebirdsql_op:op_allocate_statement(DbHandle)),
      case efirebirdsql_op:get_response(Mod, Sock) of
          {op_response,  {ok, Handle, _}} -> {ok, Handle};
          _ -> {error, "Allocate statement failed"}
      end.
  
  connect(Mod, Sock, Host, Username, Password, Database, IsCreateDB, PageSize, State) ->
      Mod:send(Sock,
  