Detecting fork using bitcoind What's the most efficient way to detect a fork in the blockchain?

My app polls for changes to user deposit addresses on each new block.

Deposits are credited to user accounts with a record in a database then the funds are transfered to cold storage.

When users get refunds those refunds come from the cold storage and a record is added to the database.

My concern is the following happening:


User deposits Btc and the database record is created.
Chain is forked from 51% attack or software incompatability.
User gets refund from cold storage but I no longer have access to the funds they sent so its out of my pocket.


It seems that everything but rechecking every deposit is marginally risky.

If I rely on height plus hash that can be manufactured by the attacker so the new chain looks like the old chain.
Maybe you can arrange it such that a refund always uses the TxOut of the transaction with which it came into the cold storage? Basically giving the user the same bitcoins back.

If a fork causes the funds not to go into cold storage they will not be going out either.