This question is common enough and complex enough to have a section of the manual all of its own. See Timing Issues and synchronisation