Fix race condition in 'dispatchMessages'

`SelectionKey#isValid` returns false if a selection key is cancelled, but if the selection key is cancelled, `SelectionKey#isAcceptable`, for example, throws a `CancelledKeyException`. Fix the method so that exceptions are properly handled.