If this is a cut and paste, you have a mistake in the first line (buffer[7] = buffer[5] should be buffer[7] = buffer[6])