Oops, sorry about the typos. I guess I didn't realize that about STDERR, I'll take that part out. (I'm actually updating someone else's code here, but that doesn't absolve me of not cleaning it up properly).
As for the \n\n after the Content-type header, why is that an error? It prints correctly. I didn't put them there specifically, but I think the original author did that to ensure that the HTTP Header was definitely "done" before starting (but I could remove it).
places 4 newlines bewteen "html" and "<!DOCTYPE", which means "<!DOCTYPE" is on the third line of the body but I think the specs requires it to be on the first line. Just leave a blank line between "html" and "<!DOCTYPE".