Wrote the function to write to the pipe depedent on message type