The starter is sort of hooked into the whole flywheel assembly. The starter has no bendix gear like a car has, but is connected to the flywheel with some special sauce that allows the starter to turn the flywheel when it engages, but to not turn when the engine is running. Same as a bendix gear, only different.
That is how it is connected to the transmission and engine, but, if you hear the solenoid click when you push the starter button, then all the electrical connections and sensors are working and the starter should turn. If you didn't hear the solenoid click, then it could be something else electrical.
Why it will start sometimes when you rock it back and forth might be an indicator to someone with more experience than me, but I'd still start at the starter. Get a service manual and find out how to test the starter, or take it out and haul it down to a starter repair house and ask them to test it for you.