moved in code from hdaemonize to fix problem with closed file descriptors