I cannot conceive of a way to prevent this from happening. It's unfortunately what the internet does, magnifies the ugly side of humanity and its not even like women can escape this treatment in the real world either. its such an ingrained flaw and the world is such a rotten place that even teaching your sons to have more respect and arming women with the ability and the fortitude to push back when they're pushed probably wont be enough. You can't culturally wipe hundreds of years of patriarchal conditioning. You live, survive and punch back as much as you can.
I totally get where you're coming from, but I think calling it a flaw is underselling the whole thing. It's a
choice. Like, literally, men have a choice to open their stupid mouths or not, and they choose to open their stupid mouths and spout this shit at women unsolicited. They feel entitled to it, and that's it's totally fine for them to treat women that way, or speak to them that way. I was raised in a bigoted, racist, conservative city. I carried a lot of that with me until I got the hell away from that place and experienced the world, and it challenged my worldview and was the best thing to happen to me. I was married to a woman that taught me so much about the struggles that women experienced in this country, and it opened my eyes about many things I was blind and ignorant to, and through our shared experience of being undervalued and mistreated (me, for my ethnicity, her, for her gender), I like to think we both grew as people, even if, in the end, our marriage didn't work out.
As a person of color, having allies call out and push back against this type of abhorrent behavior towards my people is nothing but helpful, so I imagine that women wouldn't mind having men calling out other men (including friends and family), that have such gross views when it pops up. The onus shouldn't always be on the oppressed to change the hearts of the oppressor. A lot of that change has to come from within, not just individually, but as a community. Men need to challenge men to be better men, just as much as women need to challenge men to be better men. We need to work together to enact change.
Sure, we have generations of patriarchal conditioning, but it's not an insurmountable task to stop allowing that conditioning to take root in those around us, or in the children we raise. Women have been speaking out about this shit for decades and decades. Instead of men getting defensive and plugging their ears and going "lalalalala, not all men," etc, etc, we should actually fucking listen, empathize, and fight alongside them to chip away at the rampant misogyny, sexism, and outright hatred of women that many of us let slide, especially if it's coming from our loved ones (family or friends). As has been shown time and time again, people are more inclined to listen to men (and white men, at that), when it comes to actually pausing and reconsidering their stance. In the instance of racism, a lot of bigots and racists may take a second look at their views if the person calling them out on it looks like them. I imagine it's the same with sexism.
Men have shown they aren't listening to women, so I imagine it's like screaming into the void when time and time again, women speak out and say, "hey, this shit isn't cool. stop it." But if it were a man calling out another man on their shitty behavior, it may, at the least, give them pause enough to reflect, especially if they think their friendship/relationship with you is in jeopardy because of they're behavior. I've definitely had to call out a few former friends on that shit, and the reason why they're former friends is because some of them refused to self reflect, and were therefore not the kind of people I wanted to associate with, with some actually did realize what fucking dipshits they were, and worked to improve and be better. Those people are still in my life.