Factor out handling of I/O events to reduce complexity.