Yeah, sometimes it's difficult to know the right thing to do when you're surprised by something totally out of your comfort zone and you've got only seconds to respond appropriately. Most people would indeed do what you did, OP.
But others are also on target: Quite often, the best defense is a good offense. And many who would happily bully someone by intrusively or overtly sexual behavior would not like it if the tables were turned, or even if their actions were exposed (by yelling "get away from me!" in a strong, carrying, but not panicked voice, for example, as another poster suggested). Cockroaches scurry when the light's turned on, right?
I'm just about as gay as they come, and I've had gay men come on to me inappropriately or in inappropriate settings before. I've had to maintain my space or my boundaries or (in one case) report them to gym management when they wouldn't take "no" for an answer. So yes, I'm well aware of the problem with some elements of my own subculture.
Still, simply saying, "Look, I'm aware of what you're trying to do, and I'm not interested," usually takes care of it.
Or consider confronting the perpetrator, when it's safe to do so. The person might learn from it.
NCbear (whose man was practically mobbed in the showers by other gay men when we visited a new YMCA recently


)