anchor.diff:      int events = eventsObj as int;
       for (int i = firstEvent; i <= lastEvent; i++) {
         if (((events & (1 << i)) != 0)) {
  -        if ((i == closedEvent || i == readEvent) && isClosedRead) continue;
           if (isClosing && i != destroyedEvent) continue;
  -        if (i == closedEvent && !isListening && !isClosing && !isClosed) {
  -          isClosedRead = true;
  -          issueReadEvent();
  -          continue;
  -        }
  -
  -        if (i == writeEvent) {
  -          writeAvailable = true;
  -          issueWriteEvent(delayed: false);
  -          continue;
  -        }
  -
  -        if (i == readEvent) {
  -          if (isListening) {
  -            connections++;
  -          } else {
  -            if (isUdp) {
  -              _availableDatagram = nativeAvailableDatagram();
  +        switch (i) {
  +          case readEvent:
  +            if (isClosedRead) continue;
  +            if (isListening) {
  +              connections++;
  +              if (!isClosed) {
  +                // If the connection is closed right after it's accepted, there's a
  +                // chance the close-handler is not set.
  +                var handler = readEventHandler;
  +                if (handler != null) handler();
  +              }
               } else {
  -              available = nativeAvailable();
  +              if (isUdp) {
  +                _availableDatagram = nativeAvailableDatagram();
  +              } else {
  +                available = nativeAvailable();
  +              }
  +              issueReadEvent();
  +              continue;
               }
  -            issueReadEvent();
  +            break;
  +          case writeEvent:
  +            writeAvailable = true;
  +            issueWriteEvent(delayed: false);
               continue;
  -          }
  -        }
  -
  -        var handler = eventHandlers[i];
  -        if (i == destroyedEvent) {
  -          assert(isClosing);
  -          assert(!isClosed);
  -          isClosed = true;
  -          closeCompleter.complete();
  -          disconnectFromEventHandler();
  -          if (handler != null) handler();
  -          continue;
  -        }
  -
  -        if (i == errorEvent) {
  -          if (!isClosing) {
  -            final osError = nativeGetError();
  -            if (osError != null) {
  -              reportError(osError, null, osError.message);
  -            } else {
  -              reportError(
  -                  Error(),
  -                  StackTrace.current,
  -                  "Error event raised in event handler : "
  -                  "error condition has been reset");
  +          case errorEvent:
  +            if (!isClosing) {
  +              final osError = nativeGetError();
  +              if (osError != null) {
  +                reportError(osError, null, osError.message);
  +              } else {
  +                reportError(
  +                    Error(),
  +                    StackTrace.current,
  +                    "Error event raised in event handler : "
  +                    "error condition has been reset");
  +              }
               }
  -          }
  -        } else if (!isClosed) {
  -          // If the connection is closed right after it's accepted, there's a
  -          // chance the close-handler is not set.
  -          if (handler != null) handler();
  +            break;
  +          case closedEvent:
  +            if (isClosedRead) continue;
  +            if (!isListening && !isClosing && !isClosed) {
  +              isClosedRead = true;
  +              issueReadEvent();
  +              continue;
  +            } else if (!isClosed) {
  +              // If the connection is closed right after it's accepted, there's a
  +              // chance the close-handler is not set.
  +              var handler = closedEventHandler;
  +              if (handler != null) handler();
  +            }
  +            break;
  +          case destroyedEvent:
  +            assert(isClosing);
  +            assert(!isClosed);
  +            isClosed = true;
  +            closeCompleter.complete();
  +            disconnectFromEventHandler();
  +            var handler = destroyedEventHandler;
  +            if (handler != null) handler();
  +            continue;
           }
         }
       }
  
positive.code:     var e =
          createError(error, message, isUdp || isTcp ? address : null, localPort);
      // Invoke the error handler if any.
      if (eventHandlers[errorEvent] != null) {
        eventHandlers[errorEvent](e, st);
      }
      // For all errors we close the socket
      close();
  
negative.code:     }
    }
  
    void setHandlers(
        {void Function()? read,
        void Function()? write,
        void Function(Object e, StackTrace? st)? error,
        void Function()? closed,
        void Function()? destroyed}) {
      readEventHandler = read;
      writeEventHandler = write;
      errorEventHandler = error;
      closedEventHandler = closed;
      destroyedEventHandler = destroyed;
    }
  
    void setListening({bool read = true, bool write = true}) {
  