             _dispatcher = new EventDispatcher();
         }
 
-        private function handleSocketData(e:ProgressEvent):void {
-            if (_nextMessageLen != -1 && _dataStream.bytesAvailable >= _nextMessageLen) {
-                readNextPayload();
-            } else if (_nextMessageLen == -1 && _dataStream.bytesAvailable >= 5) {
-                readNextHeader();
-            }
-        }
-
-        private function readNextHeader():void {
-            _nextMessageType = _dataStream.readByte();
-            // FEBE encodes the message length as including the size
-            // of the length field itself. This does not work well for
-            // our purposes--we subtract the size of the length field.
-            _nextMessageLen = _dataStream.readInt() - 4;
-            if (_dataStream.bytesAvailable >= _nextMessageLen) {
-                readNextPayload();
-            }
-        }
-
-        private function readNextPayload():void {
-            // TODO: We should not have to copy the message to its own array: we
-            // should wrap the stream in something that sets a 'dummy' limit to
-            // the number of bytes this particular message can read.
-            var messageBytes:ByteDataStream = new ByteDataStream();
-            _dataStream.readBytes(messageBytes, 0, _nextMessageLen);
-
-            var nextMessage:IBEMessage = new backendMessageTypes[String.fromCharCode(_nextMessageType)]();
-            nextMessage.read(messageBytes);
-            dispatch(nextMessage);
-            _nextMessageType = -1;
-            _nextMessageLen = -1;
-            if (_dataStream.bytesAvailable >= 5) {
-                readNextHeader();
-            }
+        private function handleSocketData(e:Event):void {
+        	while ((_nextMessageLen == -1 && _dataStream.bytesAvailable >= 5) ||
+        	       (_nextMessageLen != -1 && _dataStream.bytesAvailable >= _nextMessageLen)) {
+                if (_nextMessageLen == -1) {
+                	_nextMessageType = _dataStream.readByte();
+                    // FEBE encodes the message length as including the size
+                    // of the length field itself. This does not work well for
+                    // our purposes--we subtract the size of the length field.
+                	_nextMessageLen = _dataStream.readInt() - 4;
+                } else {
+		            // TODO: We should not have to copy the message to its own array: we
+		            // should wrap the stream in something that sets a 'dummy' limit to
+		            // the number of bytes this particular message can read.
+		            var messageBytes:ByteDataStream = new ByteDataStream();
+		            _dataStream.readBytes(messageBytes, 0, _nextMessageLen);
+		
+		            var nextMessage:IBEMessage = new backendMessageTypes[String.fromCharCode(_nextMessageType)]();
+		            nextMessage.read(messageBytes);
+		            dispatch(nextMessage);
+		            _nextMessageType = -1;
+		            _nextMessageLen = -1;
+                }
+	        }
         }
 
         public function send(message:IFEMessage):void {

        private static const LOGGER:ILogger = Log.getLogger("org.postgresql.febe.MessageBroker");

        public static const backendMessageTypes:Object = {
            /*
                Note that authentication messages are *not* in this
                map since they all share a single message type and
                so must be treated distinctly
            */
            'R': AuthenticationRequest,
            'K': BackendKeyData,
            'B': Bind,

            'n': NoData,
            'N': NoticeResponse,
            'S': ParameterStatus,
            '1': ParseComplete,
            'Z': ReadyForQuery,
            'T': RowDescription
        }
