There is no perfect time. You can take it whenever you want:
After your workout: Your muscles will then soak up the nutrition for muscle recovery and growth.
Before Your workout: If you're taking a shake with carbs (like banana), it will give you extra energy and also reduce exposure to muscle damage.
First thing in the morning: Taking a protein shake first thing in the morning is a good way to get essential nutrients after eight hours of sleep. There are also a number of great ways you can use protein powder besides shakes, including in the recipes for protein pancakes, or cookies.