protected function encodeMessage($msg)
    {
        $encodings = array(
            9  => '&#009;',
            10 => '&#010;',
            13 => '&#013;',
            32 => '&#032;',
            34 => '&quot;',
            39 => '&apos;',
        );

        $ret = array();
        for ($j = 0; $j < strlen($msg); $j++) {
            $str = $msg[$j];
            $asci = ord($str);
            if (isset($encodings[ $asci ])) {
                $ret[] = $encodings[ $asci ];
            } elseif ($asci >= 128 || $asci < 32 || in_array($str, array('*', '#', '%', '<', '>', '+' ))) {
                $ret[] = strtoupper('%' . sprintf("%02s", dechex($asci)));
            } else {
                $ret[] = $str;
            }
        }

        return implode('', $ret);
    }