 
   updateTypedMessageBufferOnDidCreateNewLine: ->
     if @typedMessageBuffer?
+      unless @typedMessageCurrentLineBuffer.match(/^<message\s+.*type=.*>$/)? \
+          or @typedMessageCurrentLineBuffer.match(/^<head>$/)? \
+          or @typedMessageCurrentLineBuffer.match(/^<\/head>$/)? \
+          or @typedMessageCurrentLineBuffer.match(/^<body>$/)? \
+          or @typedMessageCurrentLineBuffer.match(/^<\/body>$/)? \
+          or @typedMessageCurrentLineBuffer.match(/^<\/message>/)?
+        # escape special characters
+        @typedMessageCurrentLineBuffer = @typedMessageCurrentLineBuffer\
+          .replace(/&/g,'&amp;')
+          .replace(/"/g,'&quot;')
+          .replace(/'/g,'&apos;')
+          .replace(/</g,'&lt;')
+          .replace(/>/g,'&gt;')
       @typedMessageBuffer += "#{@typedMessageCurrentLineBuffer}\n"
       if @typedMessageCurrentLineBuffer.match(/^<\/message>/)?
         typedMessage = @readTypedMessage(@typedMessageBuffer)

  ## Writing typed messages to the terminal ------------------------------------

  writeTypedMessage: ({type,head,body,data}={}) ->
    if head or body
      startTag  = "<message type=\"#{type}\""
      startTag += " data-#{key}=\"#{value}\""  for key,value of data

        @emitter.emit('did-start-reading-typed-message')

  readTypedMessage: (buffer) ->
    console.log buffer

    typedMessageXml = $($.parseXML(buffer)).find('message')
    typedMessage = {id: @constructor.getTypedMessageId()}

