Or, he may not like the kitty litter you re using. Some cats also object to scented litters. Remember, though it may smell pleasant and fresh to you, his sensitive nose thinks it s a perfume explosion. Most cats are neat freaks, so cleaning the box out every day will help. Some particularly finicky cats may want you to clean it even more than that.
Some cats also prefer to use separate boxes for urinating and defecating.
Try setting out two boxes and see if that helps. Finally, you can make sure the box is away from his food and in a secluded corner, where he feels safe and doesn t have to be on guard. Actually, he was just getting a little overstimulated. Sometimes when a cat is really enjoying being petted, he gets overwhelmed and his instincts tell him to get rid of that nervous energy by snapping at you. Instead, withdraw the stimulus completely.