I made a sockbuf alias that sends text out in lines so that no single line will exceed a certain number of characters.. but of course color doesn't carry over.

I am trying to find a way to make the color carry over to each line, but I can't think of a way to do this despite looking in the help file for a while now.
Is there a way to check a variable for the presence of $chr(3) (the color code character), then get the LAST occurence of it in the string, plus up to 2 numbers that come after it?

Code:
  
alias sockbuf {
  var %buf = $null, %string = $2-, %num = 0
  while (%string) {
    %buf = $gettok(%string,1,32)
    %string = $deltok(%string,1,32)
    if (%num != 0) { %buf =  $+ $chr(32) $+  $+ %buf }
    while ($len(%buf) < 75 && %string) {
      %buf = %buf $gettok(%string,1,32)
      %string = $deltok(%string,1,32)
    }
    if ($len(%buf) > 75) { %string = $instok(%string,$gettok(%buf,$numtok(%buf,32),32),1,32) | %buf = $deltok(%buf,$numtok(%buf,32),32) | sockwrite -n $1 %buf }
    else { sockwrite -n $1 %buf }
    inc %num
  }
}


-------------
I am the self-appointed God of needlessly complex mIRCscript.