Clean up fd after use

If application is using many sessions and runs for a long time it fails with Errno::EMFile too many open files error. It is happening because STDOUT.dup creating new file descriptor, but STDOUT.reopen only reassociates fd#1 with given filepath but not removes created file descriptor.
This pr fixes the problem.