apply @LukasReschke's cookie changes, hopefully finally fix #854

This supersedes @LukasReschke’s cookie-past branch, hopefully fixing #854.

Please test, everyone who was involved in the original issue: @tanghus @karlitschek @RandolfCarter @almyz125 @mehturt