Validate fd before calling read/write/close. Fixes #6.