Build cookie support part I. Fixes #6, #11, #12
Build cookie support part II. Fixes #6, #11, #12

(mpr changes committed separately for easier backport).