Adding log flush
Refactor daemoninze to fix file descriptor errors.

For some unknown reason, the daemon proces crashes after the first
processed message when all file descrptors are closed.
Only closing stdin/stdout/stderr and no other fds seems to fix it.
That's probably a violation of the daemon spec from Stevens, but
it works for now.