Fixes for ts-2954

Fixes for the DNS cache poisoning bug. 

Changes correspond to the ts-2954-final.patch attached to that bug.