IPhone keeps getting stuck sideways?

It first happened not recently but a while ago that the screen was stuck sideways and wouldn't change. I ended up having to shake the phone hard enough for it to go back to normal. Now it's happened a multiple times in the past 2-3 days. What is makin my iphone 4 do this & how to prevent it from happening continuously?

You have to get rid of that trash phone and get a real phone