It looks like they are bending correctly (knee towards the front) to me. The IK solver will default to the direction the joint is bent in edit mode. So if you want to flip it, just bend that joint in the other direction in edit mode. Once you have flipped the joint, adjusting the pole angle in the IK constraint should solve any twisting problems.