Home devices control system using gsm and arduino mega2560
For the 3333 and 4444 you are doing

buffer[m] = char(buffer[m]);


and 

buffer[p] = char(buffer[p]);


which does nothing.

You probably meant

buffer_m[m] = char(buffer[m]);


and

buffer_p[p] = char(buffer[p]);