in reply to cookie problem
I'm not an HTTP expert, but I think that the Set-Cookie header should be sent before the content-type.
lynx -dump -source http://localhost/cookie-via-CGI.cgi [download]